According to the currently accepted dynamo theory, earth’s magnetism is caused mostly by the rotating liquid outer core of the earth, partly composed of molten iron convecting through it. The theory explains the magnetic field associated with not only the earth but also the sun, stars and galaxies.
What is the cause of the earth’s magnetic field?
Originally, it was thought that the centre of the earth has magnetized iron deposits, which caused it to act like a huge permanent bar magnet. However, the theory failed to explain how the magnetic field had sustained itself for long, because even if there was magnetized iron in the core earlier, the core of the earth is so hot that any permanent magnet would lose its magnetism.