As it turns out, it was already known by some philosophers from the 1200s that certain types of rocks naturally tend to rotate to point north. However, they had no idea why: For instance, it was generally thought that compas needles were attracted by the Pole Star.
Some more serious work on compasses (e.g. discovering the 'magnetic dip') was first done by Norman, near the end of the 16th century. He, however, also didn't understand that compasses rely on the magnetic field of the Earth itself. The first to grasp this was William Gilbert, also known as the father of magnetic science. In his primary academic work, published under the name De Magnete, Magneticisque Corporibus, et de Magno Magnete Tellure or, more briefly, De Magnete he concluded - based on experiments carried out on a model planet - that the Earth itself can be regarded as a giant magnet (as is reflected in the title itself), correctly attributing it to the iron core of our planet. Thus, William Gilbert was the first to realize that Earth possesses an intrinsic magnetic field.
As an aside, it is interesting to note that Gauss was in fact the first to accurately measure Earth's magnetic field and fit it to spherical harmonics in order to understand its variations.
SourceSource (aside from Wikipedia). Note that, although the page is written by a non-scientific crank, the first part is purely informative and factual. I will try to find a better source later. For now, just ignore the part where he talks about his own research