It was Massa's third successive pole and the sixth of his career and he will start Sunday's race at Catalunya alongside the local hope from Oviedo who looked sure of the premier starting position until the final seconds of qualifying.
After Massa had clocked his best time of the part-session, Alonso was on his final flying lap but was unable to get under his rival's time, much to the dismay of a parochial flag-waving crowd.
Kimi Raikkonen, Massa's Finnish Ferrari team-mate, was third fastest ahead of Alonso's McLaren Mercedes-Benz team-mate British rookie Lewis Hamilton, 22, who was fourth fastest.
Poland's Robert Kubica qualified fifth fastest for BMW ahead of Italian Jarno Trulli in his Toyota, German Nick Heidfeld in the second BMW, Finn Keikki Kovalainen in a Renault, Briton David Coulthard for Red Bull and Italian Giancarlo Fisichella in the second Renault.
Perfect conditions
The session began again in hot and dry conditions with the air temperature recorded at 27 degrees Celsius and the track as high as 48 degrees, which were ideal conditions for qualifying.
Once the action started, the usual suspects soon dominated proceedings with Hamilton, who was fastest in the morning's practice session ahead of Alonso, among the early pace-setters.
The first part of qualifying saw the removal of the slowest six drivers and it was a surprise that these included German Ralf Schumacher in his Toyota, Austrian Alex Wurz in a Williams and Australian Mark Webber in his Red Bull.
Early exits
They joined Adrian Sutil of Germany in a Spyker, his Dutch team-mate Christijan Albers and American Scott Speed of Toro Rosso among those drivers forced to start from the back rows of the grid.
Webber's failure was particularly disappointing because his team-mate Coulthard had performed strongly and followed up his rapid performance in the morning session by ensuring himself of a place in the top ten in the afternoon.
The second mini-session, to remove the next six drivers, took out German Nico Rosberg of Williams, Brazilian Rubens Barrichello and his Honda team-mate Briton Jenson Button, Japan's Takuma Sato of Super Aguri, his team-mate Briton Anthony Davidson and Italian Vitantonio Liuzzi of Toro Rosso.