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's incredible start in Spain
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.
Spanish GP: Fernando Alonso elated at 'fantastic' home win
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
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.