Red Bull's Sebastian Vettel edged out Lewis Hamilton's McLaren to take his eighth pole position of the season at the Hungarian Grand Prix.
Red Bull broke a curfew on working overnight to overhaul Vettel's car and the changes helped him get back on top.
McLaren's Jenson Button lines up third ahead of Ferrari's Felipe Massa, who out-qualified Fernando Alonso for the first time in 2011.
Last year's winner in Hungary, Mark Webber, starts in sixth for Red Bull.
The main protagonists had been split by just half a second on Friday and it was another close battle in qualifying as Vettel edged out Hamilton by just 0.163 seconds.
"Any one of five drivers could have taken pole position," BBC F1 analyst Eddie Jordan said. "We were served up another titantic battle. Vettel has answered all the critics - he has unbelievable one-lap pace."
A decision by Red Bull on Friday to break with Vettel's car the six-hour overnight curfew, which was introduced by the sport's governing body this year to stop teams working round-the-clock on their cars, seemed to be the key to his improved performance.
There is no punishment until a team has broken the curfew four times in a season.
The world champion praised his team's hard work, adding: "I felt much more comfortable in the car. I've got my confidence back."
Webber appeared to be getting the upper hand on Vettel after taking pole position at the last two races in Britain and Germany but the Australian was left baffled by his performance in Hungary.
"It's bizarre," said Webber, who was more than half a second adrift of Vettel. "There's no way I could do that lap time, no way. Yesterday it was nip and tuck but today there is a gap. We'll have to look at it."
It later emerged that the Australian's margin was explained by him forgetting to use the DRS overtaking aid at the start of the lap, cutting his pace on the straight.
Hamilton had difficulty tuning up for qualifying as a brake problem hampered his preparations in final practice.
After the team switched to a more responsive brake ahead of qualifying, the 2008 world champion unleashed a brilliant first lap in qualifying to set the benchmark.
During the first runs on super-soft tyres, Button, Vettel and Webber failed to close down Hamilton but Vettel found the pace to relegate Hamilton to second place by just 0.163secs in the final flurry of flying laps.
Hamilton looked downhearted after the session but said: "I'm happy we can get some really good points from here. Hopefully we can challenge Sebastian.
"I don't feel that I should have been on pole.
"It was a possibility and we were quick this weekend, and in Q1 we were very competitive and in Q2 but I knew it was going to be very, very close and if I'm honest I knew it was going to be difficult to be ahead of Sebastian.
"He showed serious form. My lap started out well but it didn't finish so well and that's what happens when you put the car on the edge."
Alonso was surprisingly upbeat despite being denied fourth place by his Ferrari team-mate.
"It's OK," said the Spaniard, who was 0.015secs slower than the Brazilian. "Maybe fifth is better than fourth as here it is quite a big difference (in grip) on the grid. Let's hope McLaren can put pressure on Sebastian in the first corner.
"We need to have consistent pace all through the race but it's still very open."
Nico Rosberg will start in seventh for Mercedes ahead Adrian Sutil's Force India and his Mercedes team-mate Michael Schumacher.
Sergio Perez made it through to the top-10 for the first time but did not set a fastest time after choosing to save a set of super-soft tyres for the race and will start in 10th. Force India's Paul di Resta lines up behind the Sauber.
"I was quite happy with the car and maybe I could have been more adventurous," said Di Resta. "In terms of our long-run performance we should be quite optimistic."
A late run from Williams driver Pastor Maldonado had squeezed Sebastien Buemi's Toro Rosso out of the first phase of qualifying.
A hangover from the German GP means Buemi will now start in 23rd after receiving a five-place grid penalty as punishment for his collision with Nick Heidfeld's Renault.
"We could not aim for better than 16th but tomorrow with three sets of super-soft tyres I hope I can score points," said Buemi.
An intriguing internal team battle at Team Lotus ended with Heikki Kovalainen edging out Jarno Trulli. Despite being beaten for the ninth time by Kovalainen, Trulli was happier with his car after a new power steering system was fitted to the Lotus.
Qualifying position is deemed to be of even greater importance at the Hungaroring where it can be difficult to overtake.
But this year the moveable rear wing (DRS), Kers power-boost and tyre strategy are expected to help the drivers move through the field.