Pivoting Magnets

Yesterday in the lab I was trying to find out whether any parts of my equipment have magnetic components that could interfere with the performance of our electron microscope.

When I was putting them aside I noticed that the magnets (made from Neodymium) were always aligning in one direction. And no, it is not toward a north-south line. And upon rotation they pivot until the kinetic energy is wasted and come to rest in this orientation.

After making this short video I found out within a minute the reason for this nice effect. Can you figure it out, too?