Magnets align themselves according to the magnetic field around them. When freely suspended, north pole of a magnet point towards the magnetic south pole (geographical north pole) of Earth and it happens due to the magnetic field of Earth. When a strong magnet is brought close to it, it realigns according to the resultant magnetic field.