<h2>
Answer: the core of the earth</h2>
The explanation accepted by science until now, indicates that Earth's magnetic field is generated in the deepest of the terrestrial nucleus.
This is due to the movements of liquid metals in the core of the planet.
To explain it in more detail:
Right in the center of the Earth there is an internal core of solid iron at a very high temperature, this iron is as hot as the surface of the Sun, however, the pressure caused by the gravity force prevents it from becoming liquid. Around this inner core is the outer core, a layer of approximately 2000 km thick, composed of iron, nickel and small amounts of other metals in liquid state. Due to the lower pressure of the outer core, in relation to the inner core, the metal here is fluid (molten).
The differences in temperature, pressure and composition within the outer core cause convection currents in the molten metal, as the cold, dense matter sinks, and the warm, less dense matter rises. In addition, the Coriolis force, resulting from the spin of the Earth, also causes eddies in this mixture of molten metals.
This is how the movement of liquid iron inside the Earth generates electric currents, which in turn produce magnetic fields, and the spiral caused by the Coriolis force causes the magnetic fields to align in approximately the same direction, being this combined effect added to produce a vast magnetic field that envelops the entire planet.
This field extends from the terrestrial nucleus, progressively attenuating itself in outer space. However, it should be noted that unlike the field of a bar magnet, the Earth's magnetic field changes over time because it is actually generated by the movement of the iron alloys in the Earth's outer core, which causes the poles to move.