The ancients figured out how to make a compass and how to use it to find north. But they would never have been able to figure out why it worked.
That was left to English scientist William Gilbert, who was the first to describe the Earth itself as a magnet in his important work De Magnete, published in 1600. The rest is modern science.