Potential energy of a magnetic moment at an angle $\theta$ with the magnetic field $=- MB \cos \theta$
Thus work done in rotating from angle $\theta_{1}$ to $\theta_{2}$
$= MB \left(\cos \theta_{1}-\cos \theta_{2}\right)$
$= MB \left(\cos 0^{\circ}-\cos 90^{\circ}\right) $
$= MB (1-0) $
$= MB$