In the Eastern European country of Sokovia, the Avengers – Tony Stark, Steve Rogers, Thor, Bruce Banner, Natasha Romanoff, and Clint Barton – raid a Hydra outpost led by Baron Wolfgang von Strucker, who has been experimenting on humans using the scepter previously wielded by Loki. They encounter two of Strucker's experiments – twins Pietro Maximoff, who has superhuman speed, and Wanda Maximoff, who can manipulate minds and project energy – and apprehend Strucker, while Stark retrieves Loki's scepter.
Stark and Banner discover an artificial intelligence within the scepter's gem, and secretly use it to complete Stark's "Ultron" global defense program. The unexpectedly sentient Ultron, believing he must eradicate humanity to save Earth, eliminates Stark's A.I. J.A.R.V.I.S. and attacks the Avengers at their headquarters. Escaping with the scepter, Ultron uses the resources in Strucker's Sokovia base to upgrade his rudimentary body and build an army of robot drones. Having killed Strucker, he recruits the Maximoffs, who hold Stark responsible for their parents' deaths by his weapons, and go to the base of arms dealer Ulysses Klaue to obtain Wakandan vibranium. The Avengers attack Ultron and the Maximoffs, but Wanda subdues them with haunting visions, causing the Hulk to rampage until Stark stops him with his anti-Hulk armor.
A worldwide backlash over the resulting destruction, and the fears Wanda's hallucinations incited, send the team into hiding at a safehouse. Thor departs to consult with Dr. Erik Selvig on the meaning of the apocalyptic future he saw in his hallucination, while Romanoff and Banner plan to flee together after realizing a mutual attraction. However, Nick Fury arrives and encourages the team to form a plan to stop Ultron. In Seoul, Ultron forces the team's friend Dr. Helen Cho to use her synthetic-tissue technology, together with vibranium and the scepter's gem, to perfect a new body for him. As Ultron uploads himself into the body, Wanda is able to read his mind; discovering his plan for human extinction, the Maximoffs turn on Ultron. Rogers, Romanoff, and Barton find Ultron and retrieve the synthetic body, but Ultron captures Romanoff.
The Avengers fight amongst themselves when Stark secretly uploads J.A.R.V.I.S. – who is still operational after hiding from Ultron inside the Internet – into the synthetic body. Thor returns to help activate the body, explaining that the gem on its brow – one of the six Infinity Stones, the most powerful objects in existence – was part of his vision. This "Vision" and the Maximoffs accompany the Avengers to Sokovia, where Ultron has used the remaining vibranium to build a machine to lift a large part of the capital city skyward, intending to crash it into the ground to cause global extinction. Banner rescues Romanoff, who awakens the Hulk for the battle. The Avengers fight Ultron's army while Fury arrives in a Helicarrier with Maria Hill, James Rhodes and S.H.I.E.L.D. agents to evacuate civilians. Pietro dies when he shields Barton from gunfire, and a vengeful Wanda abandons her post to destroy Ultron's primary body, which allows one of his drones to activate the machine. The city plummets, but Stark and Thor overload the machine and shatter the landmass. In the aftermath, the Hulk, unwilling to endanger Romanoff by being with her, departs in a Quinjet, while the Vision confronts Ultron's last remaining body.
Later, with the Avengers having established a new base run by Fury, Hill, Cho, and Selvig, Thor returns to Asgard to learn more about the forces he suspects have manipulated recent events. As Stark and Barton also leave, Rogers and Romanoff prepare to train new Avengers: Rhodes, the Vision, Sam Wilson, and Wanda.