The ancients figured out how to make a compass and find north with it. But they could not figure out why it worked.
Much later, English scientist William Gilbert tried. He was the first to explain that the whole Earth is a magnet. His important book, De Magnete, came out in 1600. The rest is modern science.