Mega Evolution's lore was heavily retconned in ORAS to be part of Hoenn's history and as a result most of the lore connecting it to Kalos was thrown in the garbage. ZA tried to bring back some of the original lore from XY but it's just made a bigger mess, so now we have two distinct backstories for Mega Evolution that are selectively canon depending on the game:
- XY - Mega stones were mutated from evolution stones when the UW was fired 3000 years ago in the Kalos region, the first Mega was a Lucario whose trainer found the Mega and Key stones.
- ORAS - A meteoroid struck Hoenn 1000 years ago, awakening Groudon and Kyogre and making them fight. Hoenn's ancient people prayed for Rayquaza to intervene, and it used the people's wishes and the meteoroid's energy to Mega Evolve. No, this does not explain how Pokemon other than Rayquaza can Mega Evolve, why do you ask?
If Mega Evolution originated from a meteoroid that fell in Hoenn, then why are machines built in Kalos 2000 years prior so connected to the phenomenon? Hoennians wuz Megas and shieet.
ORAS has a bigger explanation, but you have to piece a bunch of info together and read between the lines. So basically, there's two relevant types of energy: natural energy and life energy. The Earth is full of natural energy (like, literally inside it), and since meteorites can be conceptualized as like mini planets, meteorites also contain a smaller amount of natural energy inside them. Meteorites that crash onto Earth can then break off pieces full of natural energy, that can somehow become attuned to a specific Pokemon species' life energy (aka Infinity Energy) and become a Mega Stone. Keystones are created from the same source, but they're attuned to human life energy. When you Mega Evolve, the stones are acting like a way for you and your Pokemon's life energy to connect to form a burst of power (imagine the energy flowing between trainer and Pokemon in an infinity symbol pattern),
A way for these meteorite pieces to be have their natural energy converted into Pokemon life energy is for a massive blast of life energy from a legendary to wash over them. The wave of energy given off by the Ultimate Weapon in Kalos 3000 years ago created the Mega Stones that are found in Kalos, and the second firing of it in XY created more, which are the Mega Stones you can only find in the post-game. Kyogre and Groudon letting off a wave of energy when they Primal Reverted during the story of ORAS had the same effect, which is where the Mega Stones you can only find in-game after the Kyogre/Groudon event came from.
In ORAS, a giant meteor hit where Sootopolis is thousands of years ago, which opened up a rift in the Earth where natural energy can flow out of, which is the Cave of Origin. Groudon and Kyogre can use this massive natural energy for Primal Reversion (called that because natural energy was somehow more plentiful millions of years ago). It's never really stated where the Red and Blue Orbs came from, but what Team Magma/Aqua are trying to do at Mt. Chimney is shove a bunch of natural energy from the volcano into the meteor they stole to turn it into a new Orb, so presumably the Orbs are similar to Mega Stones but containing raw natural energy that Groudon and Kyogre can use, They probably either came from the Cave of Origin, or from the meteor that opened up the Cave of Origin.
Speaking of meteors, our only known sources of Keystones are the Anister sundial and the meteor that fell to Sootopolis, the former of which is also said to have fallen from space. Perhaps being in cities is what caused them to become attuned to human life energy and make Keystone? Don't know. But the meteor that fell to Sootopolis was the Keystone used in Mega Evolving Rayquaza thousands of years ago, when the people of the dragon tribe prayed to the stone. Rayquaza doesn't need a Mega Stone because it feeds off meteorites and converts their natural energy directly into its own life energy it can use to Mega Evolve. Hoenn is constantly pelted in meteorites at a greater rate than other regions, and combined with events with the legendaries that blast natural energy out, is why Hoenn has as many Mega Stones as Kalos.