Work done in rotating a magnet is given by
$ W=\int_{0}^{\theta}\tau d\,\theta$
where $\tau$ is torque and $d\theta$ angular charge
Also, $ \tau = MH\, \sin\, \theta $
$\therefore W=\int_{0}^{\theta} MH \sin\, \theta d\theta$
$\Rightarrow W=MH\int_{0}^{\theta} \sin\, \theta d\theta$
$\Rightarrow W=MH[-\cos\theta]^{\theta}_{0}$
$\Rightarrow W = MH[ - \cos \theta + \cos 0] $
$\Rightarrow W=MH[1-\cos\theta] $