On Earth the magnetic poles are near the geographic poles.
- Does Earth have this situation by pure luck?
- What is the situation on other planets?
It is not coincidence and it does not apply to just the Earth.
The Sun, Earth, Jupiter, Saturn, Uranus, and Neptune all have sizable magnetic fields. Mercury and Ganymede have smaller but still noticeable magnetic fields. All of these bodies have one thing in common: They have a sizable amount of rotating, electrically conductive fluid somewhere beneath the surface.
The underlying theory of why those objects that do have a magnetic field do have them is magnetic dynamo theory. This theory suggests that the object's rotational and magnetic axes need to be a bit offset from one another.
There are some enigmas that dynamo theory still needs to explain. Saturn's magnetic field is almost perfectly aligned with it's rotation axis. Uranus and Neptune have magnetic fields that are tilted by a bit too much, and the center of their fields are offset from the center of the planet by a considerable amount.
On the other hand, Venus, the Earth's Moon, Mars have negligible magnetic fields, despite all three apparently having partially liquid cores. What they most likely don't have is a convective liquid core. These three bodies have a stagnant lid: no active vulcanism, no plate tectonics. This keeps heat bottled up inside and keeps the liquid core from roiling.