Ferrari's Fernando Alonso sent his home crowd into raptures by beating Lotus driver Kimi Raikkonen to win the Spanish Grand Prix.
He took his second victory of the year and moved into third place in the championship with a four-stop strategy while Raikkonen did three.
Ferrari's Felipe Massa was third and Red Bull's Sebastian Vettel fourth.
Mercedes' Nico Rosberg and Lewis Hamilton faded to sixth and 12th after starting from the front row.
The Mercedes cars struggled with high tyre usage, their bugbear for several years, and were unable to hold onto their positions at the head of the field.
Rosberg, who did three stops, managed his tyres better than Hamilton, who made four, but was unable to sustain a competitive pace.
"You feel the support from everyone and it helped," said Alonso. "We want to do well here in front of our fans. We did it and we are happy for that but we don't want to stop here obviously."
Alonso laid the foundations for his victory by moving up from his fifth place on the grid to third with a superlative passing move on the first lap, overtaking both Raikkonen and Hamilton around the outside of the fast Turn Three.
Alonso trailed Rosberg and Vettel until the first pit stops, during which he leapfrogged Vettel, and took the lead from Rosberg again around the outside into Turn One at the start of lap 13.
From then on, the question was whether Alonso could build enough of a lead to enable him to make an extra pit stop and still beat Raikkonen.
He did so comfortably, rejoining from his third stop on lap 36 just five seconds behind Raikkonen, both with one stop remaining and Alonso on much fresher tyres.
It took Alonso only two laps to catch and pass Raikkonen, from where the Spaniard cruised to the 32nd victory of his career, moving him ahead of Nigel Mansell into fourth place in the all-time winners' list behind Michael Schumacher, Alain Prost and Ayrton Senna.
It also significantly boosted his championship hopes, closing his deficit to leader Vettel from 30 to 17 points. Raikkonen stays second, just four points behind the German.
Hamilton slipped from third to fourth place in the championship, but is 22 points behind Alonso and only five ahead of Massa.
After losing the lead to Alonso, Raikkonen was left comfortably ahead of Massa, who made a bid to close in on the Finn before Raikkonen responded with six laps to go to show he was in control.
"Obviously we want to win and it is disappointing to finish second but sometimes you have to take what you can get," said Raikkonen. "Hopefully we can achieve the big goal in the end."
Vettel appeared for a while to be trying the same three-stop strategy as Raikkonen, stopping at similar times to the Finn in the early part of the race.
But without the race pace to challenge for the lead, he made a fourth stop only three laps after Alonso's and had to settle for fourth.
"We just didn't have the tyres to fight with the guys in front," Vettel said. "The start was good and the beginning of the race didn't look too bad, but then we tried to hold on to the three-stop a little bit too much and had to admit towards the end that we wouldn't make it. We can be happy with fourth."
Vettel's team-mate Mark Webber took fifth place on a four-stop strategy that had him in last place on lap eight following an early stop, the Australian moving back through the field with a measured drive as the race progressed.
Scot Paul di Resta and Force India continued to impress with a typically smooth, understated drive to seventh place.
Di Resta closed right in on Rosberg in the final laps, but failed to pass the Mercedes, despite getting alongside him on the run to Turn One with three laps to go.
Jenson Button, meanwhile, moved up from his poor 14th place on the grid on a three-stop strategy to finish eighth, just ahead of team-mate Sergio Perez, who did four from eighth on the grid, while Toro Rosso's Daniel Ricciardo took the final point in 10th.
1. Fernando Alonso (Spa) Ferrari 1hr 39mins 16.596secs
2. Kimi Raikkonen (Fin) Lotus +00:09.338
3. Felipe Massa (Brz) Ferrari 00:26.049
4. Sebastian Vettel (Ger) Red Bull 00:38.273
5. Mark Webber (Aus) Red Bull 00:47.963
6. Nico Rosberg (Ger) Mercedes 01:08.020
7. Paul di Resta (GB) Force India 01:08.988
8. Jenson Button (GB) McLaren 01:19.506
9. Sergio Perez (Mex) McLaren 01:21.738
10. Daniel Ricciardo (Aus) Toro Rosso 1 lap
11. Esteban Gutierrez (Mex) Sauber 1 lap
12. Lewis Hamilton (GB) Mercedes 1 lap
13. Adrian Sutil (Ger) Force India 1 lap
14. Pastor Maldonado (Ven) Williams 1 lap
15. Nico Hulkenberg (Ger) Sauber 1 lap
16. Valtteri Bottas (Fin) Williams 1 lap
17. Charles Pic (Fra) Caterham 1 lap
18. Jules Bianchi (Fra) Marussia 2 laps
19. Max Chilton (GB) Marussia 2 laps
ret. Jean-Eric Vergne (Fra) Toro Rosso 14 laps
ret. Giedo van der Garde (Ned) Caterham 45 laps
ret. Romain Grosjean (Fra) Lotus 58 laps