In 2018, twenty-three days after Thanos erased half of all life in the universe, Carol Danvers rescues Tony Stark and Nebula from deep space and they reunite with the remaining Avengers—Bruce Banner, Emil Blonsky, Steve Rogers, Thor, Natasha Romanoff, and James Rhodes—and Rocket on Earth. Locating Thanos on an uninhabited planet, they plan to use the Infinity Stones to reverse his actions, only to find that Thanos has already destroyed them and the Gauntlet, thus preventing any further use. Enraged, Thor decapitates Thanos.
Five years later, Scott Lang escapes from the Quantum Realm. Reaching the Avengers Compound, he explains that he experienced only five hours while trapped. Theorizing that the Quantum Realm allows time travel, they ask a reluctant Stark to help them retrieve the Stones from the past to reverse the actions of Thanos in the present. Stark, Rocket, Banner, and Blonsky, the latter two having been able to merge their intelligence with the Hulk's strength (with Blonsky becoming a third Hulk being known as "Maestro"), build a time machine. Banner notes that altering the past does not affect their present; any changes create alternate realities. Banner and Rocket travel to Norway, where they visit the Asgardian refugees' settlement New Asgard and recruit an overweight and despondent Thor. In Tokyo, Romanoff recruits Clint Barton, who became a vigilante after his family was erased during the execution of Thanos' plan.
Banner, Blonsky, Rogers, and Stark time-travel to New York City during Loki's attack in 2012. At the Sanctum Sanctorum, Banner and Blonsky convince the Ancient One to give them the Time Stone after 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 Camp Lehigh 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; Rocket extracts the Reality Stone from Jane Foster, while Thor gets encouragement from his mother, Frigga, and retrieves his old hammer, Mjølnir. Barton, Romanoff, Nebula, and Rhodes travel to 2014; Nebula and Rhodes go to Morag and steal the Power Stone before Peter Quill can, 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 of 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 have built. Banner, who has the most resistance to their radiation, uses the gauntlet to undo every one of Thanos's disintegrations. Meanwhile, 2014-Nebula, impersonating her future self, uses the time machine to transport 2014-Thanos and his warship to the present, which he then uses to destroy the Avengers Compound. 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 Mjølnir-wielding Rogers, and summons his army 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' army. Danvers also arrives and destroys Thanos' warship, but Thanos overpowers her and seizes the gauntlet. Stark steals the Stones and uses them to disintegrate Thanos and his army, despite killing himself in doing so.
Following Stark's funeral, Thor appoints Valkyrie as the new king of New Asgard and joins the Guardians. Rogers returns the Stones and Mjølnir 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. In a series of mid and post-credits scenes, J. Jonah Jameson of broadcasts doctored footage of the Battle of London in which Quentin Beck frames Spider-Man for the drone attack and his death before exposing Spider-Man's secret identity to the world, much to Peter Parker's shock; Nick Fury and Maria Hill are revealed to be the Skrulls Talos and Soren in disguise, under orders from the real Fury while he is away in space commanding a group of Skrulls; S.W.O.R.D. director Tyler Hayward reactivates "The Vision", the now all-white, reassembled original body; Hayward is arrested while Monica Rambeau is informed by a Skrull that a friend of her mother's wants to meet. In a post-credits scene, Wanda hears the twins cry for help while studying the Darkhold in her astral form; John Walker builds a new shield from scrap metal and his war medals; after receiving a full pardon, Sharon Carter rejoins the CIA and intends to use this access to sell government secrets and resources; and Loki awakens surrounded by several other Loki variants.
