In the Eastern European country of Sokovia, the Avengers - Tony Stark / Iron Man (Robert Downey Jr), Steve Rogers / Captain America (Chris Evans), Thor (Chris Hemsworth), Bruce Banner / Hulk (Mark Ruffalo), Natasha Romanoff / Black Widow (Scarlett Johansson), and Clint Barton / Hawkeye (Jeremy Renner) - raid a Hydra outpost commanded by Baron Wolfgang von Strucker (Thomas Kretschmann), who has been experimenting on humans using the sceptre previously wielded by Loki.
They encounter two of Strucker's experiments: the Maximoff twins Pietro (Aaron Taylor-Johnson), who has superhuman speed, and Wanda (Elizabeth Olsen), who has telekinetic abilities, and can manipulate minds and project energy. Rogers captures Strucker, while Stark retrieves the sceptre after Wanda implants a fear-filled vision of a bleak possible future in his mind.
Back at the team's headquarters - Avengers Tower (formerly Stark Tower) - Stark and Banner discover an artificial intelligence within the sceptre's gem, and secretly plan to use it to complete Stark's "Ultron" global defence program. After many failed attempts, they leave for a victory party to celebrate the recapture of the sceptre. Unbeknownst to them, the program comes to life...
The unexpectedly sentient Ultron (James Spader) awakens, believing that he must eradicate humanity to save the Earth, and overwhelms Stark's AI, JARVIS (Paul Bettany). In the guise of one of Stark's Iron Legion robot drones, Ultron attacks the Avengers as they relax together after their party. Escaping with the sceptre, Ultron uses the resources in Strucker's Sokovia base to upgrade his rudimentary body and build an army of robot drones. After killing Strucker, he then meets and recruits the Maximoff twins, who hold Stark responsible for their parents' deaths due to weapons manufactured by his company.
The trio goes to the base of arms dealer Ulysses Klaue (Andy Serkis) in an African shipyard to obtain Wakandan vibranium. The Avengers pursue them there, but Wanda subdues all of them except Stark and Barton with haunting visions, causing Banner as the Hulk to rampage through a nearby city until Stark stops him with his Hulk-buster armour. A worldwide backlash over the resulting destruction and the fears incited by Wanda's hallucinations send the team into hiding at Barton's safehouse, where they meet his wife and children, who had been kept a secret from all of them except Romanoff.
Seeing no answers to be found there, Thor departs to consult with his friend Dr Erik Selvig (Stellan Skarsgard) on the meaning of the apocalyptic future he saw in his hallucination, while Romanoff and Banner discuss the possibility of fleeing together after realising their mutual attraction. However, Nick Fury (Samuel L Jackson) arrives and encourages the team to fight back and stop Ultron.
In Seoul, Ultron forces the Avengers' friend and ally Dr. Helen Cho (Claudia Kim) to use her synthetic-tissue technology, together with vibranium and the sceptre's gem, to perfect a new body for him. As Ultron uploads himself into the synthetic body, Wanda is then able to read his mind, and discovers his plan to annihilate humanity. Horrified, the Maximoffs turn on Ultron and join forces with Rogers to distract him while Romanoff and Barton retrieve the synthetic body. However, Ultron captures Romanoff while Barton reluctantly takes the body back to Stark and Banner at Avengers Tower, followed by Rogers and the twins.
The Avengers fight amongst themselves when Stark secretly uploads JARVIS - who is still operational after hiding from Ultron deep within 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" (Paul Bettany) - proving himself a worthy ally by effortlessly lifting Thor's hammer Mjolnir - and the Maximoff twins accompany the Avengers back 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 and cause global extinction. Banner rescues Romanoff, who pushes him into a pit to rouse the Hulk for the battle.
The Avengers face off against Ultron's army of drones, while Fury arrives in a Helicarrier with Maria Hill (Cobie Smulders), James Rhodes / War Machine (Don Cheadle) and other SHIELD agents to evacuate the civilians. While the other Avengers go to help with the evacuation, Wanda accepts the task to guard the machine from activation by Ultron's drones. However Ultron steals the Avengers' Quinjet and goes on a strafing run in a desperate attempt to stop the evacuation.
Pietro is killed when he shields Barton from Ultron's gunfire. The Hulk leaps into the Quinjet and forcibly evicts Ultron - leaving him vulnerable to a vengeful Wanda, who abandons her post to destroy Ultron's primary body. This then allows one of his drones to activate the machine. The city begins to plummet, but Stark and Thor overload the machine and destroy the landmass, with the Vision swooping in to rescue Wanda. The Hulk, unwilling to endanger Romanoff by being with her, departs in the Quinjet, while the Vision confronts and destroys Ultron's last remaining body.
Later, the Avengers have established a new base run by Fury, Hill, Cho, and Selvig. Barton returns home to his family, and Thor returns to Asgard to learn more about the forces which he suspects have manipulated recent events, while Stark also leaves the team. Rogers and Romanoff prepare to train the new team of Avengers: James Rhodes, the Vision, Sam Wilson / Falcon (Anthony Mackie), and Wanda.
In a mid-credits scene, Thanos, dissatisfied by the failures of his pawns, dons the Infinity Gauntlet and vows to retrieve the Infinity Stones personally...