At the end of The Avengers, Thor returns to Asgard with Loki and the Tesseract. When Avengers first came out, it seemed that S.H.I.E.L.D. simply allowed Thor to take them; trusting that Asgard would be a better place to both give justice to Loki and to keep the Cube safe.
However, during Avengers: Endgame, we find out that S.H.I.E.L.D. (specifically the Hydra part of S.H.I.E.L.D.) did not want to allow Thor to take Loki; Secretary Pierce specifically tells Thor that they want Loki.
This argument takes place without any changes having happened due to interference from the 2023 Avengers. So it would seem that this always happened in the prime timeline, it was just off-camera in the original Avengers movie.
So given that we now know that S.H.I.E.L.D. was not willing to let Thor take Loki (and I think they wanted to keep the cube as well, though I don't remember if this was specifically being also fought over), how should we interpret the end of Avengers?
I see a few different options:
- Thor eventually won the debate with Pierce and convinced him of his point (unlikely as Pierce was evil).
- Thor simply took advantage of the fact that he is more powerful and took Loki and the cube against S.H.I.E.L.D.'s wishes.
- Someone else—such as Fury—intervened and took Thor's side (also seems unlikely as Pierce outranks even Fury).
- The scene where Thor takes Loki back to Asgard happened some time later, after S.H.I.E.L.D. finished questioning Loki. After all, Pierce did say that Thor could have him when they were done with him.
Looking for either the most likely in-universe explanation that makes sense, or any out-of-universe sources that address this.