When rocks form and cool, metal particles align according to the magnetic poles.
The metal particles are affected by the magnetism of the poles, more specifically the north pole which is the stronger magnetic field. The metal particles act like the needle in the compass and align themselves to the north pole.