If magnetic force does not exert work on moving charges, how could a current loop immersed in a magnetic field rotate and how could a magnet attract or repel another one?
The Lorentz force is always perpendicular to the motion of the charges. No escape from that. This particular problem where it looks like the magnetic force has done the work, is explained beautifully and in detail in Griffiths, "Introduction to Electrodynamics". As an analogy, Griffiths also points out a similar problem in elementary mechanics.I think the key aspect here is that the magnetic field doesn't do work on *freely moving* charges. That is, at any given point in time, the force exerted on the charge is perfectly perpendicular to the motion (courtesy of the Lorentz force).
In a current loop, or in a magnet, the charges are constrained in their movement. That means, the Lorentz force and the charge's motion is no longer perfectly perpendicular, with the result of work being done.
Mind you, I'm only 95% sure on this. But it seems in line with this site's explanation: