This complicated movement is due to the nature of the Earth’s metallic core, which is an iron-nickel alloy. There’s an inner solid core, and an outer liquid core. Both of these conduct, but it’s the movable, liquid metal core that generates much of what we observe.
The centre of the earth is hotter than points outside it. This means that the liquid core will experience convection currents, which will set up electric currents moving around. These electric currents generate magnetic fields — and as they’re changing, these magnetic fields generate an electric field. As everything is moving, these electric and magnetic fields exert a force on the charges that are flowing in currents. You get this complicated, nonlinear behaviour where everything interacts with everything else — and the end result is a large magnetic field.
This area of physics is called magnetohydrodynamics, and it’s notoriously difficult to deal with everything accurately in computer models. The slightest perturbation can have wide-ranging effects on your solutions, and it’s technically difficult to solve the generated equations on a computer.
The actual mechanism by which the reversal of the magnetic poles occurs is not very well understood (as this area is so hard), but a group in Japan managed to reproduce this behaviour of the core in their computer models (in 2002).
The earth’s magnetic fields are generated by dynamo action from the earth’s molten metal core. The molten core experiences convection currents, and these moving currents in turn generate a magnetic field.
But the convection currents in the earth’s core are very turbulent. In fact, they might only be describable by chaos theory. This is because there are so many factors affecting the system (the core is non-uniform, the earth is also rotating, the gravitational field is not uniform, there are various layers of different densities, there is the combination of extreme heat and pressure). One property that often occurs in chaotic systems is what’s called a “strange attractor”, where small changes can cause the system to suddenly and apparently “randomly” flip between two stable states (really pseudo-randomly: we could predict the randomness if we had perfect information).
As Jack says, this has been replicated in computer models of the earth’s core. The process of flipping is complex: it can take between 1,000 and 10,000 years.