Explanation:
There must be differences in temperature, pressure and composition within a liquid metal core inside the planet, since this produces a motion of the liquid metal, generating electric currents, which produce magnetic fields, that align, combining their effect to produce a large field Magnetic that envelops the planet.