The short answer: It was all Loki, all along.
Loki has always been among my favorite Marvel characters. The God of Mischief, for sure. But also someone who is treacherous, cunning, one to run a complex game, and certainly one to manipulate others into doing his bidding. But the MCU version of Loki, though brought to life on screen wonderfully by Tom Hiddleston, has been noticeably underpowered. Maybe that’s just a byproduct of the story telling in the MCU, or maybe that’s all been part of Loki’s long con.
Limiting the analysis to just what we’ve seen in the MCU, here’s what we know about Loki. Odin raised him as his son, and Loki clearly believed he had a shot at succeeding Odin as the ruler of Asgard and the Nine Realms. That is, of course until he finds out in Thor that he’s an adopted Frost Giant, and that Odin has been planning for Thor to succeed him for years. But Loki has ambition, and he’s not going to let his oafish brother take the throne that easy. With his brother banished to Earth in mortal human form, Loki concocts a plan to sneak the Frost Giants into Asgard to kill Odin during his Odin-sleep, betray the Frost Giants by killing Lauffey as he murders Odin, thereby starting war between Asgard and Jotunheim. Let the plotting begin.
Of course we’re also introduced to both the Tesseract and the Infinity Gauntlet in Thor. Whether truly a fake or just a retcon, the implication here is that at one point in his reign, Odin was actively pursuing the Infinity stones. Thor may not understand everything (or anything) about the stones at this point in his life, but it’s very likely Loki does. Thor is the warrior, the jock, the party boy. Loki is the kid who survived by maxing out his mind -- magic, lore, strategy, etc. We know from the opening of Captain America that Odin once possessed the Space stone in the form of the Tesseract and sent it to (left it in?) Norway a millennium ago. We also know from Thor: The Dark World, that Odin's father, Bor, once took the Reality stone (in the form of the Aether) from the Dark Elves and hid it away. Knowing the family history with at least two of the stones, and knowing that Odin’s vault includes a relic for controlling the Infinity stones, you can bet Loki has studied them.
"Someone has been playing an intricate game, and has made pawns of us."
And that leads us to the first Avengers movie. Loki has successfully escaped justice on Asgard. He still craves the throne, but he’s also learned that Thanos already has the Mind stone in his possession, and he’s looking for more. Now Thanos would never attack Asgard, and Odin, directly without the stones. But with Odin getting on in years, and needing to enter Odin-sleep more often, a powered-up Thanos and his Black Order would be a severe threat to Loki’s plans for rule.
So how do you take an Infinity stone from Thanos? Just as one does not simply walk into Mordor, one does not simply take an Infinity stone from Thanos. But, he might be convinced to give someone the stone -- someone who, perhaps, knew the location of another stone, and offered to lead an invasion to take it and return it to Thanos. Of course, Loki had no intent to ever make Thanos more powerful. And he never had any intention of taking the stones for himself, not yet anyway. Further, if he were to take the Mind stone from Thanos -- and the Space stone from Earth -- and then not hand them back to Thanos, he’d be on the run for the rest of his days. So what better plan than to lose the Battle of New York on purpose? If that were to happen, then Thanos would lose control of both the Mind stone and the Space stone, and in theory each would probably be better protected than before.
Great plan so far. But now assume that when Loki commandeers the minds of several SHIELD agents with the sceptre at the beginning of Avengers, he realizes that SHIELD has been infiltrated by Hydra? Maybe now there’s a chance to help Hydra sow chaos on Earth (thereby keeping the Avengers occupied) by making sure that when he loses the battle, the sceptre falls into the hands of compromised SHIELD agents. Now he’s starting to clear the field ahead of him, and he’s done it by simply losing the battle on purpose!
"You faked your own death. You stole the throne, stripped Odin of his power, stranded him on Earth -- to die -- releasing the Goddess of Death."
With Odin weakening, Thanos de-stoned, and the Avengers battling Hydra and each other, it’s time to worry about Hela. He knows that when Odin goes, she’s coming back, and she was a handful for Odin even at his full strength. So he speeds Odin towards his end with magic (our first hint at how powerful Loki really is), takes the throne disguised as Odin, and basically waits for things to get so bad that Thor rushes back to Asgard. In the meantime, he’s learned that Hulk is on Sakaar and hatched a plan to use Thor and Hulk to take out Hela. So he travels to Earth with Thor just in time to see Odin pass, Hela arrive, and then he “foolishly” opens the bifrost. And how is it that Loki and Thor just happen to land on the same planet where Hulk has been hanging out, bashing heads for two years? There's really no reason to assume this was pure chance. Thor and Loki crash off the rainbow bridge at different points in both time and space, and yet both arrive on the same planet in a far-off star system. And whatever Loki's story about arriving "weeks ago", it's just as likely he's visited Sakaar before via one of the secret pathways between worlds that he found on Asgard.
Oh yeah, and Loki knows the Ragnarok prophecy too. That’s his wild card. With Hulk, Thor, Hela, and Surtur all battling on Asgard, he’s sure that one of them (and probably more) aren’t going to make it out. The most important thing for Loki is to make sure he survives the fracas with the Space stone in his possession. And sure enough, he does. So by the end of Thor: Ragnarok, Odin is dead. Hela seems to be lost in Surtur’s destruction of Asgard. Surtur’s job is done, and he’s off to whatever demons of prophecy do once they’ve completed their fated tasks. The Avengers are broken. Thanos has none of the Infinity stones. Things are looking up for the God of Mischief.
But Thanos and Thor still sit between Loki and his goal of ruling the Nine Realms (and Asgard, wherever it’s people land). So there’s still some thinning that needs to be done. Thanos isn’t going to stop trying to collect the stones, and none of the Avengers — Thor included — understand the danger like Loki does. So given the choice of backing Thanos or the Avengers, Loki puts his money on Thanos. Loki cuts a deal with Thanos to survive the snap in exchange for the Space stone -- and a little bit of theatre ensues to sell the con. He’s betting that Thanos will power up, defeat the Avengers, and then retire with his task accomplished. After all, Thanos’s goal is not intergalactic rule. It’s “saving” the universe from itself. With that task complete, Thanos doesn’t really care who rules over whom. That takes us to the end of Infinity War. Thanos is successful and looking to retire to the sunsets on a nice planet that feels like Titan once did. Half of the Avengers have been decimated. All that’s left is to take care of Thor, and then it’s on to ruling the Nine Realms.
“If we can’t protect the Earth, you can be damned-well sure we’ll avenge it.”
So that brings us to Endgame. As Tony Stark told Loki in The Avengers, “if we can’t protect the Earth, you can be damned-well sure we’ll avenge it!” And that’s where we are. In my version of Endgame, the remaining Avengers defeat a tired, wounded, unprepared Thanos; undo the snap; then then come face-to-face with Loki and…? That raises an important question. How do you create a situation where all of the now-restored Avengers and Guardians can be thrown into battle, in a way that creates drama in the outcome? Maybe Galactus, but that's challenging both from the standpoint of licensing (IIRC, Galactus was part of the Fantastic Four property licensed to Fox at the time of writing and filming) and the trap of ever-escalating baddies. So then what? Here’s a clue: we saw his head and gleaming-red eyes in Spider-Man: Homecoming. That’s right, Loki resurrects Ultron (remember, he never does the heavy lifting himself), and with Loki's help, Ultron creates a new drone army to battle Team Good at the grand finale. The Avengers and the Guardians prevail, of course. But also of course, Loki escapes in the end, left to fight another day.
That's how I'd play the story out. It provides a nice story arc for Loki, and it brings him more in line with the character we've seen in print. It also provides the "Marvel twist" on the villain. We've been lead to believe that Thanos was the bad guy from the original Avengers on -- that he was controlling and manipulating Loki, Hydra, and Ultron to advance his own agenda -- while in reality it was Loki pulling all the strings. I see it as a nice way to wrap up the character arcs of heroes and villain alike.