Potential Energy of a Magnetic Dipole (bar magnet) in a Magnetic Field:
We know that the magnitude of a torque acting on a dipole is-
This torque tends to align the dipole in the direction of the field.The work to be done in rotating the dipole against the action of the magnetic torque. This work done is stored as potential energy of the dipole.