Lewis Hamilton on pole position in Spain ahead of Sebastian Vettel
Mercedes' Lewis Hamilton edged out Ferrari's Sebastian Vettel to take pole position for the Spanish Grand Prix.
Hamilton failed to improve on his final run, but his first lap was good enough to beat Vettel by 0.051 seconds.
A mistake by the German in the final corner could have been crucial as Mercedes' Valtteri Bottas took third ahead of Ferrari's Kimi Raikkonen.
Home hero Fernando Alonso produced an exceptional effort to take seventh place for McLaren-Honda.
- Predict your top 10 race result
- Hedonism, history and Arnie's neck - F1 heads to Barcelona
- Rain in Spain? Your F1 forecast
Hamilton bounces back
Hamilton's superb effort was exactly what he needed after a difficult weekend last time out in Russia, where he finished fourth.
He set the pace in final qualifying with a one minute 19.149 seconds lap, and was just 0.025secs slower on his second run.
That gave Vettel a chance, but he locked up into the last chicane and missed out by just 0.051secs.
Hamilton's pole also owed a lot to a major upgrade the team brought to the race, with a narrower nose cone and major aerodynamic changes around the front of the car.
It was the team's first big upgrade of the season and seemed to have cancelled out two upgrades Ferrari brought to Russia and this race, which were smaller individually but seem to have been worth about the same amount overall.
Vettel rewarded for going with his gut
Vettel still had reason to be thankful - after a last-minute engine change before qualifying, he was told to stop the car on track by his engineer on his first lap of qualifying.
Vettel questioned the decision, asking: "Are you sure?" He was told to try to bring the car back to the pits. But a change of engine settings got the car running properly and Vettel was able to continue.
Bottas was just 0.224secs behind Hamilton despite missing three-quarters of final practice in the morning because of an engine change, edging out Raikkonen by 0.066secs.
Alonso shows his class after Friday embarrassment
Red Bull were fifth and sixth, with Max Verstappen beating team-mate Daniel Ricciardo by nearly half a second and providing evidence that an aerodynamic upgrade had closed the gap to the top two teams.
Verstappen was just 0.557secs off pole position - about half the deficit Red Bull have had over the first four races of the season.
But their progress was overshadowed by Alonso's superlative effort in beating both Force Indias and Felipe Massa's Williams, cars with a Mercedes engine that has at least 100bhp more than McLaren's Honda.
His performance suggests McLaren might be strong at Monaco, which raises doubts about the wisdom of their best asset missing the race and being replaced by Jenson Button, whose motivation to return to F1 after his retirement is being questioned by sources close to the team and driver.
However, Alonso said he had "zero regrets" about missing Monaco.
Alonso said: "Zero regrets. I will race the Indy 500, one of the best or the biggest race in the world.
"There are six cars - two Mercedes, two Ferraris, two Red Bulls - that will be unbeatable for the next couple of races. So to fight for P7 in Monaco? No thanks."
Jolyon Palmer - the other British driver on the grid - qualified 17th, knocked out in the first session in which he was just under 0.4secs slower than team-mate Nico Hulkenberg, who is 13th on the grid.
"Yesterday felt good," Palmer said. "Today I have struggled. I don't really know why. I just didn't have the pace at all."
What they said
Hamilton: "First Q3 lap was very, very good. The last lap was not quite as good. I was up by 0.2secs I think, but I didn't finish it that way.
"I didn't make a mistake, but it is very gusty out there and sometimes you brake in the same place and the car stops really well or locks up and I braked and the car really stopped [too quickly]. But it was enough to keep me ahead."
Asked whether his mistake was the difference, Vettel said: "I'm afraid it was, yeah. Always the last chicane is a tricky one for me. The second run was really good up to that final chicane."
Alonso, who is missing the next race in Monaco to race in the Indianapolis 500, said: "Maybe running the ovals I learned how to go quick in the straights. It was a good qualifying for us and P7 is a gift.
"Today was a beautiful day, a beautiful qualifying in which we were finding tenth after tenth. Then surprisingly we made it into Q3, and we had another very good lap.
"The important thing is tomorrow, to try to get a few points."