Energy Conservation when two permanent magnets attract each other I have a very basic doubt related to energy conservation in a system of magnets. Suppose I have two magnets (bar magnets) of magnetic moment $M$ kept along the line with an initial distance $X_0$ such that they attract. (North of one facing south of another ). One is stationary and the other is free to move. Now by intuition it seems that the energy of the system will decrease as the movable magnet comes close and the decreased energy will be kinetic energy of the movable magnet.
Now suppose if we replace the magnets with two loops of equivalent magnetic moment $M$ of area $A$ and constant current $I$ (s.t. $M=I A$), one loop fixed and the other loop free to move. The loops will still attract each other. The direction of current in loops is in same sense so that they attract each other just like magnets. As the movable loop comes closer by say amount $dx$, the flux in each loop due to other will increase (in the direction of the current) so the emf produced in both loops will oppose the current. So to maintain constant current a supply must provide the energy to both loops. Let's call this work done by supplied in both loops $dW_e$ . Now as the movable loop comes close, mutual inductance also increases, so the total energy stored in themagnetic field will also increase  ($E_{\rm field} = 2*0.5 LI^2 + MI^2$). Let's call the change in magnetic field energy $dW_f$. Let $F$ be the force exerted on the movable loop so mechanical work done be $dW_m$, so $dW_e= dW_f+dW_m$, also these are positive as loops come closer.
Now my question is why energy in field in first scenario decreases and in second case increases as the magnets or equivalent current loops come closer, after all a permanent magnet can be replaced by a current carrying loop? Or why  the permanent magnets attract each other even if the stored energy in field increases as they attract?
 A: 
Now my question is why energy in field in first scenario decreases and in second case increases as the magnets or equivalent current loops come closer, after all a permanent magnet can be replaced by a current carrying loop?

The quick answer is, total energy in the current loop system increases due to source of energy needed to keep the currents constant. However, this total energy is equal to magnetic field energy only in the current loop system. In the ferromagnet system, we have to distinguish energy of magnetic field, and energy of the system.
You did not give a convincing reason for the view that energy of magnetic field
$$
E_B = \int \frac{1}{2\mu_0}B^2dV
$$
decreases in the first case. You assume it does, perhaps based on the idea that work done by the system equals decrease of its total energy $E$. But total energy of a system of magnets $E$ is not the same as the above magnetic field energy $E_B$. $E_B$ is energy ascribed to magnetic field $\mathbf B$ alone, ignoring energy of matter; it is thus not total energy $E$ of the system magnetic field + matter. The latter energy should be dependent not only on the magnetic field $\mathbf B$, but also on details of the physical state of the magnets, such as their magnetization, temperature and internal stress.
In the simplest case where nothing of these can change, we can perhaps use (for total energy of the system) the familiar formula
$$
\int \frac{1}{2}\mathbf B\cdot \mathbf H~dV
$$
where $\mathbf H = \mathbf B/\mu_0 - \mathbf M$. If so, the integrand is equal to integrand of $E_B$ outside the magnets, but inside the magnets, density of total energy is
$$
\frac{1}{2\mu_0}B^2 - \frac{1}{2}\mathbf B\cdot \mathbf M.
$$
In our case of magnets with aligned orientations, we have $\mathbf B$ inside magnets pointing close to the same direction as $\mathbf M$, and as the magnets get closer to each other, the field-matter term remains negative but its magnitude gets bigger. This way, we can see that while magnetic field energy $E_B$ gets bigger, total energy can get lower, if the effect of the field-matter term is strong enough.
So it is quite possible that as magnets approach each other and do work, total energy $E$ decreases, while the magnetic field energy $E_B$ increases. If magnetization does not change as the magnets approach each other (the simplest model of the ferromagnet), $E_B$ indeed has to increase, the same way it does in the case of two loops with currents that are constant in time. The ferromagnet system - with geometry and current equal to those of the current loop system - produces equal magnetic field $\mathbf B$.
In the case of two current loops, keeping currents constant requires electromotive force working against the induced emf, and thus source of energy, such as battery, connected and supplying the energy to both the magnetic field and the mechanical work done. If currents stay the same as they get closer to each other, then there is more energy in the magnetic field, and this energy came from the battery. And in this case, since ideal current loops do not require matter interacting with the field, magnetic field energy gives the total energy of the system.
A: If I understand correctly, in the first scenario you consider the mechanical work of the magnets, I’ll write it
$$
\delta W_m = F\cdot dx
$$
The force is conservative, so you can assume that it derives from a potential $E_m$ given by:
$$
\delta W_m = -dE_m
$$
Since work is positive $\delta W_m >0$, the force being attractive and the magnets get closer, you get a decrease of mechanical energy: $dE_m <0$.
In the second scenario, I think that there are some confusions in the formulas, even though the end result is correct. Just to make things clear, you assimilate the magnets as fixed currents, I'll call them $1$ and $2$. The variation of energy of the magnetic energy:
$$
E_B=\int d^3x \frac{1}{2\mu_0}(B_1+B_2)^2 = \frac{1}{2}L_1I_1^2+ \frac{1}{2}L_2I_2^2+MI_1I_2
$$
is given by the total work:
$$
\delta W_B = -dE_b = \delta W_e+\delta W_m
$$
with $\delta W_e,\delta W_m$ the electric and mechanical work respectively (note that your formula is different). They are given by (no need to take into account the self inductance as the currents do not change):
$$
\delta W_e = -I_1d\phi_{12}-I_2d\phi_{21}
$$
with $\phi_{ij}$ the flux of the field of $j$ through the current $i$, and $\delta W_m$ given by the same expression as in the first paragraph. Since the flux increases, $\delta W_e<0$ so $\delta W_B < \delta W_m$ which is the discrepancy you were complaining about. Actually, it turns out that:
$$
\delta W_e = -2\delta W_m \tag{1}
$$
so you even get $\delta W_B = -\delta W_m>0$ which seems truly contradicting.
To answer the first question, yes you can assimilate magnets as arising from bound currents.
For the second question, currents do attract despite the increase of magnetic energy. The reason is mathematically analogous to thermodynamics. The second principle says that equilibrium is reached at maximum entropy for a closed system. However if you have a thermostat with which the system can exchange energy, you should rather introduce free energy and equilibrium is reached at the minimum of free energy.
The correspondence is made exact if you map entropy to magnetic energy, current to (inverse) temperature, energy to flux, and mechanical energy to free energy. Indeed, since you need electric work to maintain current, your system is open, and equilibrium is characterised by a minimum of mechanical energy. Mathematically, this amounts to doing a Legendre transform. In particular since the magnetic energy is quadratic in flux, the Legendre transform is its opposite, ie $E_B = -E_m$ and this is how you get $(1)$.
For your question in the comment about the physical origin of the electric energy source to maintain currents, the answer lies in quantum mechanics. Essentially, the Bohr Van-Leeuwen theorem states that classically you cannot have magnetism arising from classical theory, so a purely classical model of permanent magnets with currents with an electric source is impossible. However, this difficulty can be overcome using quantum mechanics. In particular, you can assimilate your permanent as quantum particles with non zero spin. Your currents have no more need for an electric energy source as there constant by definition and cannot be "turned off."
Hope this helps.
