How the potential energy of the dipole is $m.\vec{B}$ when work done by any magnetic field is Zero? We know that magnetic fields don't do any work on ANY CHARGE from the form of Lorentz force we have here. But we still have magnetic dipole getting oriented by external magnetic fields. Now, intuition says that it has to be the currents in the magnetic dipoles which are causing some electric fields to get induced which in turn produces a magnetic field and so on. Something similar to this has come up in the past Is the work done by magnetic field always zero?. But the answer given here is very vague as to what really happens in microscopic physics. I want to know what exactly is happening in the view of classical electrodynamics and how the two pictures are not contradicting each other.
On The Side Note
I have faced similar problems before too in which I am comparing two pictures in electrodynamics and couldn't move forward without stamping out the assumptions of either of the theories/pictures. Like in this case, we have derived all the electromagnetism (I think!) from the point of view of a macroscopic world in which a constant current is driving the magnetic field. And the net charge inside such current flow is zero. All the microscopic interactions, on average, give rise to these macroscopic phenomena. So telling in some situations that you really have to go to sub-atomic to explain this phenomenon seems wrong. So, there shouldn't be any difference between microscopic and macroscopic physics as we took care of all those while averaging out.
 A: Magnetic dipole vs magnetic moment
You ask about magnetic dipole, but you mean magnetic moment due to current loops. Strictly speaking, magnetic dipole is a collection of two magnetic poles with non-zero magnetic dipole moment (the Coulomb magnetic moment). It is a useful model of a magnet from the Coulomb theory of magnetostatics, where magnetic field can do work on magnetic poles; in this theory, force on physical magnetic dipole made of two particles with magnetic pole charge $q,-q$ at positions $\mathbf r_1,\mathbf r_2$ is
$$
\mathbf F = q \bigg(\mathbf H(\mathbf r_1) -\mathbf H(\mathbf r_2) \bigg).
$$
Deriving the formula for potential energy for such a dipole in magnetic field
$$
E_p = - \mathbf m \cdot \mathbf B
$$
is easy, the derivation is just like that for potential energy of an electric dipole in external field in electrostatics.
When you ask how
$$
E_p = - \mathbf m \cdot \mathbf B
$$
is consistent with the idea that magnetic field cannot do work on charged particles (due to the Lorentz formula), you are asking about magnetic moment that is due to a current loop, not about magnetic moment due to magnetic dipole (which belongs to the theory above).
Energy of magnetic moment in external magnetic field
The potential energy formula
$$
E_p = - \mathbf m \cdot \mathbf B
$$
is still valid in a limited sense for systems where the magnetic moment $\mathbf m$ is due to a current loop, but it is harder to derive. It is easier to derive it in special cases, such as magnetic moment due to loop of current.
Work of external forces on a current loop in changing external magnetic field
Consider a planar current loop, placed in an external field $\mathbf B_{ext}$ that we can control. Let the loop be in unstable position in the magnetic field (held in place by some external forces), so that magnetic field is $\mathbf B_{ext,1}$ (rougly in the direction of the $z$-axis), the current is $I_1$ (negative), $\mathbf S = A\mathbf e_z$ is the surface area vector of the circuit, and magnetic moment is $\mathbf m_1 = I_1 \mathbf S = I_1 A \mathbf e_z$ (roughly of opposite orientation to the magnetic field).
Then during a small time interval $\Delta t$ we slowly increase the external magnetic field by a little to a value $\mathbf B_{ext,2} = \mathbf B_{ext,1} + \Delta \mathbf B_{ext}$.
Due to this change, during that time the current will experience induced electric field due to external sources $\mathbf E_{i,ext}$ and entire circuit will experience induced EMF that boosts the current in the original direction. As this proceeds, the external induced electric field will do work on the current, and since this is a conservative system, this work will get stored in the system (as increased magnetic energy). So we can use this work to define potential energy:
$$
\text{increase of potential energy when going from state 1 to state 2} := $$
$$
\text{work of external induced electric force on the current}.\tag{**}
$$
Formally
$$
E_{p,2} - E_{p,1} = \Delta W.
$$
We can express the work done as function of magnetic field and magnetic moment of the current loop. The procedure could be as follows.
Work done during $\Delta t$ can be expressed as the product
$$
\Delta W = \mathscr{E}_{ext} I_1 \Delta t
$$
where $\mathscr{E}_{ext}$ is external induced EMF. This is, due to Faraday's law, equal to minus rate of change of magnetic flux due to external magnetic field through the circuit:
$$
\mathscr{E}_{ext} = - \frac{\Delta\mathbf B_{ext} \cdot \mathbf S}{\Delta t}
$$
Combining the two last equations, we get
$$
\Delta W = - \Delta\mathbf B_{ext} \cdot \mathbf S I_1.
$$
However the circuit has, initially, the magnetic moment
$$
\mathbf m_1  = I_1 \mathbf S.
$$
So the work done is
$$
\Delta W = - \Delta\mathbf B_{ext} \cdot \mathbf m_1.
$$
Defining potential energy for a "frozen" magnetic moment
If the current loop system is such that current (and thus also magnetic moment) stays almost the same during all the changes of external field we do, we can approximate and say the only changing term on the right-hand side of work done (above) is the external magnetic field. This may be the case when there is already a much greater current in the system than the current changes we can cause by the external magnetic field. Then we can define the following potential energy function of the external magnetic field:
$$
E_p (\mathbf B_{ext}) = - \mathbf m \cdot \mathbf B_{ext}. \tag{*}
$$
As long as $\mathbf m$ changes only negligibly during all changes of magnetic field we do, this potential energy obeys (**). This is somewhat applicable to a ferromagnet in a weak external magnetic field. Its magnetization and magnetization current is almost " frozen" and changes only very little due to weak external field.
The correct potential energy for the current loop in perfect conductor
The potential energy derived above is not valid for an ordinary current loop when the current is entirely induced by changes in external magnetic field.
In general, if the magnetic moment changes are partially non-conservative (e.g. the changes of magnetic moment produce some heat inside), then there is no potential energy that can be defined.
However, if our current loop system is conservative (perfect conductor), exact potential energy function can still be derived. We start with the expression for work done during infinitesimal change:
$$
d W = - d\mathbf B_{ext} \cdot \mathbf m.
$$
Now, if both $\mathbf B_{ext}$ and $\mathbf m$ were functions of the same quantity, we could integrate this and find the correct potential energy function.
From Kirchhoff's second circuital law applied to perfect conductor, we have
$$
\mathscr{E}_{ext} + \mathscr{E}_{self} = 0,
$$
$$
\frac{dB}{dt} A = - L\frac{dI}{dt}
$$
where $L$ is self-inductance of the circuit and $B$ is normal component of $\mathbf B_{ext}$ effective in calculating the magnetic flux. From this and the initial condition $I=0, B=0$ we find that magnetic field is a function of current:
$$
B = - \frac{L}{A}I.
$$
Similarly, magnetic moment magnitude is function of current $m = IA$. So now we can express the work done as
$$
dW = d\bigg(\frac{1}{2}L I^2\bigg)
$$
which suggests that potential energy is
$$
E_p =  \frac{1}{2}L I^2.
$$
This can be expressed in terms of external field and magnetic moment:
$$
E_p = -\frac{1}{2}\mathbf m \cdot \mathbf B_{ext}.
$$
This energy expression is half of that given by the sought-for formula (*). The different factor is due to fact that during the process of increase of magnetic field, magnetic moment in
$$
- \Delta \mathbf B_{ext} \cdot \mathbf m
$$
is built up  from zero. Work on zero or very small magnetic moment as field changes by $\Delta B_{ext}$ is almost zero, but in the end, work on the final magnetic moment is much larger. Magnetic moment is a linear function of the external magnetic field, so net work is one half of the product of the final values.
A: This question can be addressed differently if you are working with the macroscopic or the microscopic Maxwell’s equations.
If you use the macroscopic equations then the Lorentz force law is more complicated (M. Mansuripur. Physical Review E 79, 026608, pp 1-10. 2009): $$\mathbf F = \rho_f \mathbf E + \mathbf J_f \times \mu_0 \mathbf H + (\mathbf P \cdot \nabla)\mathbf E + (\mathbf M \cdot \nabla) \mathbf H + (\partial \mathbf P/\partial t)\times \mu_0 \mathbf H -(\partial \mathbf M/\partial t)\times \epsilon_0 \mathbf E$$ where it is clear that work can be done by the magnetic field $\mathbf H$ and not just by the electric field $\mathbf E$.
If you use the microscopic equations then the question does not arise in a fundamental sense because there is no magnetization on which to do work. However, with the microscopic equations you could make a magnetic dipole from a current loop.  Then, the question becomes how to account for the work done on a current loop.
The microscopic Poynting's theorem work term $\mathbf E \cdot \mathbf J$ can be expanded by transforming to the rest-frame of the loop. Assuming that $v\ll c$ the transformation equations are:
$$\mathbf E' = \mathbf E + \mathbf v \times \mathbf B$$
$$\mathbf J' = \mathbf J - \rho \mathbf v$$ where the primed quantities are quantities in the rest frame of the loop. Substituting those into $\mathbf E \cdot \mathbf J$ and simplifying, we get:
$$\mathbf E \cdot \mathbf J = \mathbf E' \cdot \mathbf J' + \mathbf v \cdot (\rho \mathbf E + \mathbf J \times \mathbf B)$$
So, that means that the $\mathbf E \cdot \mathbf J$ term itself contains within it the mechanical work due to the magnetic field. If you pull out the non-mechanical work, then the mechanical work is exactly what you would expect from the macroscopic Lorentz force law, including a term from the magnetic field.
The idea is that an expression like $\mathbf E \cdot \mathbf J$ can actually include work done by the magnetic field. The fields are highly inter-related and depend on each other, so it is just a matter of investigating those dependencies.
A: Suppose we have some kind of static external magnetic field acting on a current loop. There are a few ways in which the overall process can result in the loop acquiring energy:

*

*The loop is lifted against a gravitational field

*The loop accelerates linearly because the magnetic field is non-uniform

*The loop accelerates angularly in the direction tending to align its magnetic dipole moment with the external magnetic field

In all of these cases, a back emf will develop in the loop (Faraday's law), tending to reduce the magnitude of the magnetic dipole moment. If our assumption is that the magnetic dipole moment is held constant, then it must be the case that the loop contains a battery and a regulator, or something of the sort. The battery does work on the charges; the magnetic field does not.
From the point of view of classical electrodynamics, a point magnetic dipole such as an electron is basically just an infinitesimal version of this, and no special treatment is required. We just take the limit as the size of the current loop goes to zero, and imagine that the regulator is so perfect that there are not even slight fluctuations in the magnitude of the magnetic dipole moment.
