How is energy conserved in resonance? According to Wikipedia, 

[...] resonance is a phenomenon that occurs when a vibrating system or external force drives another system to oscillate with greater amplitude at a specific preferential frequency.

Can anyone please explain why it happens? And how is energy conserved in resonance?
 A: Just think about how you might push a child's swing.  You apply a push once every oscillation of the swing and thus build up the amplitude of the swing.  This is a resonance condition whereas if you pushes the swing at a slightly lower frequency you would not be able to increase the amplitude of the swing as much.
Once the swing is at a constant amplitude which is achieved by you giving the swing a little push every oscillation the energy that you input into the swing during one oscillation is equal to the energy which is lost due to frictional forces during that cycle.

In general resonance is a particular example of what is called forced oscillations.
Forced oscillations are all about a system called the driveR making another system called the driveN oscillate.
Ignoring the initial behaviour (which includes the transients) the driveN system reaches steady state.
This means that it oscillates with constant amplitude at the frequency of the driveN system.
This is the driveN system undergoing forced oscillations.
If one keeps the amplitude of the driveR constant but varies its frequency one finds that the steady state amplitude of the driveN system depends on the frequency of the driver.
Resonance is said to occur when for a particular driveR frequency the amplitude of the driveN system is a maximum and that frequency is called the resonant frequency of the driveN system.
This resonant frequency is related to the natural frequency is related to the frequency of free (natural) oscillations of the driveN system.
However for amplitude resonance one must be care about this natural frequency.
Confusion often arises because there are two ways of defining the frequency of free oscillation of a system.
One is the natural frequency if there is no damping which is often called $\omega_o$ and the other is the natural frequency if there is damping and this is often called $\omega_d$.
For small damping these two frequencies are approximately the same and so often the distinction between the two is ignored.
However amplitude resonance occurs when the frequency of the driveR is equal to the natural frequency of the driveN system which if there is damping is $\omega_d$.
There are other types of resonance.
One is velocity resonance – the speed of the driveN system is a maximum and this occurs at the natural frequency of the undamped driven system whether or not there is damping.
This is also true of energy resonance which is characterised by maximum power (energy/time) being transferred from the driveR to the driveN.
In mechanical systems it is usual to talk about amplitude resonance because it is so much easier to measure a distance than a speed.
However it is current (velocity) resonance which is often referred to in electrical circuits.
A: Perhaps a better way to put this question is how is energy concentrated within a resonant system? As others have already stated energy is always conserved if one considers the universe in tallying where energy comes and goes. But if you are considering a system, defined within spatial boundaries, the system can lose or gain energy through its boundaries.
In the case of resonant systems, they are extremely efficient in concentrating energy. That is they can easily trap incoming energy - especially if that energy rides on say for example waves of the same frequency as that of the system.
So the reason resonant systems are able to concentrate this energy is by the particular structure of these systems. These systems are characterized by their resonant or natural frequency, they can easily admit energy from outside the system, and the structure is such that dissipative forces are minimized. The other attribute of resonant structures is their ability to keep the energy trapped by allowing it to flow between potential and kinetic (or electrical and magnetic) states. Resonant systems tend to trap energy.
