Twenty-three days after Thanos killed half of all life in the universe,[N 1] Carol Danvers rescues Tony Stark and Nebula from deep space and they reunite with the remaining Avengers—Bruce Banner, Steve Rogers, Thor, Natasha Romanoff, and James Rhodes—and Rocket on Earth.
Locating Thanos on an uninhabited planet, they plan to retrieve the Infinity Stones and use them to reverse his actions, but discover Thanos has already destroyed them to prevent further use. Enraged, Thor decapitates Thanos.
Five years later, in 2023, Scott Lang escapes from the quantum realm.[N 2] Reaching the Avengers' compound, he explains that he experienced only five hours while trapped. Theorizing the quantum realm allows time travel, they ask Stark to help them get the Stones from the past to reverse Thanos's actions in the present. Stark, Rocket, and Banner, who has since merged his intelligence with the Hulk's strength, build a time machine. Banner notes that changing the past does not affect their present; any changes create alternate realities. Visiting the Asgardian refugees' settlement New Asgard, in Norway, Banner and Rocket recruit an overweight and despondent Thor. In Tokyo, Romanoff recruits Clint Barton, who had become a vigilante after the death of his family.
Banner, Lang, Rogers, and Stark travel to New York City during Loki's attack in 2012.[N 3] At the Sanctum Sanctorum, Banner convinces the Ancient One to give him the Time Stone by promising to return the various Stones to their proper points in time. At Stark Tower, Rogers retrieves the Mind Stone from Hydra sleeper agents, but Stark and Lang's attempt to steal the Space Stone fails, allowing 2012-Loki to escape with it. Rogers and Stark travel to the S.H.I.E.L.D. headquarters in 1970, where Stark obtains an earlier version of the Space Stone and encounters his father, Howard. Rogers steals Pym Particles from Hank Pym to return to the present and spies his lost love, Peggy Carter.
Meanwhile, Rocket and Thor travel to Asgard in 2013;[N 4] the former extracts the Reality Stone from Jane Foster, while the latter gets encouragement from his mother, Frigga, and retrieves his old hammer, Mjolnir. Barton, Romanoff, Nebula, and Rhodes travel to 2014; Nebula and Rhodes go to Morag and steal the Power Stone before Peter Quill can,[N 5] while Barton and Romanoff travel to Vormir. The Soul Stone's keeper, Red Skull, reveals it can only be acquired by sacrificing a loved one. Romanoff sacrifices herself, allowing Barton to get the Stone. Rhodes and Nebula attempt to return to their own time, but Nebula is incapacitated when her cybernetic implants link with her past self, allowing 2014-Thanos to learn his future self's success and the Avengers' attempt to undo it. 2014-Thanos sends 2014-Nebula forward in time to prepare for his arrival.
Reuniting in the present, the Avengers place the Stones into a gauntlet that Stark, Banner, and Rocket built. Banner, having the most resistance to their gamma radiation, wields the gauntlet and reverses Thanos's disintegrations. Meanwhile, 2014-Nebula, impersonating her future self, uses the time machine to transport 2014-Thanos and his warship to the present, destroying the Avengers' compound in the process. Present-day Nebula convinces 2014-Gamora to betray Thanos, but is unable to convince 2014-Nebula and kills her. Thanos overpowers Stark, Thor, and a Mjolnir-wielding Rogers and summons his army from his warship to retrieve the Stones, intent on using them to destroy the universe and create a new one. A restored Stephen Strange arrives with other sorcerers, the restored Avengers and Guardians of the Galaxy, the Ravagers, and the armies of Wakanda and Asgard to fight Thanos's army. Danvers also shows up and destroys Thanos's warship, but Thanos overpowers her and seizes the gauntlet. Stark grabs the Stones and uses them to disintegrate Thanos and his army, but the strain of using the Stones kills him.
Following Stark's funeral, Thor appoints Valkyrie as the new ruler of New Asgard and joins the Guardians. Rogers returns the Stones and Mjolnir to their proper timelines and remains in the past to live with Carter. In the present, an elderly Rogers passes his shield to Sam Wilson.