It surely is caused by huge electric currents in Earth's iron core. But we still do not know the details of how it is produced, why it varies, and why it completely reverses itself every million years or so. Space probes have discovered magnetic fields elsewhere in the solar system, extending outward from the Sun and surrounding both some terrestrial and some gas giant planets. In truly great magnetic fields like those of Jupiter, atomic particles may be heated to millions of degrees, and a great electric arc flows between the planet and its moon lo. Each has a magnetic field, aurorae, radiation belts, and naturally-generated radio noise.
Magnetic properties vary widely and unpredictably among the terrestrial planets.
The Earth has a metal core and a strong magnetic field. Mercury has a large metal core, but only a weak magnetic field. Venus probably has a metal core, but it has no field. Mars, which may or may not have a metal core, has no field. The gas giant planets, at least the two visited by spacecraft, seem to be more uniform.