Here's why:
* Outer core: This layer is composed of liquid iron and nickel, constantly moving due to convection currents. This movement creates electric currents, which in turn generate a magnetic field.
* Inner core: While solid, the inner core's intense heat and pressure also contribute to the overall magnetic field.
The Earth's magnetic field acts as a protective shield, deflecting harmful solar radiation and protecting life on the planet.