As all things in any universe Equestria too had a beginning. In the same way a plant sometimes grows on the remains of its ancestors, Equestria grew on the remains of the world that came before it.
Scientists assume an imbalance in the magical field of the universe caused a gigantic explosion that formed all matter. That matter then gathered and formed Equestria by the virtue of its own gravitational pull. It then pulled in more and more matter until it had absorbed all material in its sphere of influence. Sun and moon got pulled into orbit and the basic foundations of life were laid. And as life tends to do, it inevitably found a way to thrive.
More religious ponies believe that Equestria was formed by an all-powerful creator. Said creator then formed the very universe in a way so they could live in it. Some believe that either Celestia or Luna or both of them did that. Others give that honour to an obscure entity they call Faust.
Neither the scientists nor the priests are correct though. But Twilight Sparkle will soon find out the truth.