It's an interesting fact that scientists don't fully understand how it works. But it seems to be to do with molten metal circulating in the core. Given that it's just liquid metal sloshing around, it seems understandable that it won't always circulate perfectly - imagine the cloud bands in Jupiter's atmosphere - they are reasonably stable but change from time to time. When the liquid changes its speed or direction, however slowly it does so, the resulting magnetic field will move or switch direction.
<span>
As scientists try to build better mathematical models of how the core works, they should be able to learn more about the magnetic field it produces. Hope this helps</span>
Examples of medium are solid ,liquid ,and gas basically matter. :D