Here's why:
* The outer core is a liquid layer of iron and nickel. The movement of this molten metal generates electric currents.
* These electric currents create magnetic fields. This is similar to how an electromagnet works.
* The Earth's rotation influences the flow of the molten metal, resulting in a complex and dynamic magnetic field.
The magnetic field is not generated in the solid inner core or the mantle, as they are not conductive enough to support the necessary electric currents.