The universe was "seeded" and/or colonized some many thousand years before the games by an extremely advanced human civilization called the "Ancients", who connected their holdings with a multitude of warp gates. In comes the Kreegans, the devils/demon as people from Erathia/Enroth call them, a kind of alien that literally cannot help jumping from one world to the next and sucking everywhere they go dry of resources because of their extremely rapid breeding. A war broke out, the Ancients were wrecked, connection with their outlying colonies broken as the Kreegans trashed the warp gate system. These worlds, without the precursors to guide them, rapidly fall into barbarism and a stereotypical medieval fantasy world is what Erathia became after one/a couple thousand years of forgetting the Ancients exist.
At the end of M&M VII, if you follow the path of Light you'll help Resurrectra, one of the Ancients crash-landing on Erathia on a spaceship conveniently called the Lincoln, to reboot the warp gate system and enabling traveling from world to world. Judging from the closing lines ("Where do we go from here?" "Oh, anywhere you wish! Maybe, one day, you'll even come across the Ancients' world yourselves" - Not exact quote) however, it's considered to be more or less impossible to get back to the Ancients' world even with that.
Of course there's a ton of inconsistencies even if you're to buy the sci-fi plot, mostly to do with the nature of magic and the gods, or where did the elves/dwarves/other fantasy races come from. As you can see, sci-fi and magical fantasy doesn't mix very well.