https://youtu.be/lWHxmJf6U3M
Watch this video, you will get a good enough idea of how the earth's magnetic fields have strengthened itself over time.
But the question as to how the fields originated in the first place, is still a question to be answered.
But I think that though with very low probability, it may have happened that once, all the metal ions would have (for very small instant) arranged in such a manner that all their magnetic moments would have added up and may be this would have been the origin (since the molten fluid inside earth is constantly flowing) and then this field would have been enhanced by the process explained in the video.