Lewis Hamilton beat Sebastian Vettel to pole position at his Mercedes team's home German Grand Prix.
Hamilton knocked the world champion off top spot with the very last lap of qualifying by 0.103 seconds.
Red Bull's Mark Webber was third from Lotus's Kimi Raikkonen, Romain Grosjean and Toro Rosso's Daniel Ricciardo.
Fernando Alonso will start eighth, one place behind team-mate Felipe Massa, after Ferrari qualified on the slower 'medium' tyre with a view to the race.
The 'soft' tyre on which all those ahead of the Ferraris will start begins to rapidly lose pace after about five or six laps whereas the medium runs for more than 20.
Alonso will be hoping to move into the lead as the front-runners pit early and end up with a better finishing position as a result.
But the strategy did not work for Vettel when he tried it in China earlier this year, a decision Alonso later described as "strange".
Hamilton produced a remarkable return to form after struggling badly throughout the three practice sessions this weekend.
Before qualifying Mercedes said they did not know how Vettel could have been more than 0.6secs ahead of their fastest driver, Nico Rosberg in final practice. Hamilton was 0.5secs slower again.
Hamilton said: "We were miles off this morning, we made a change and I didn't know if it would work but it did."
Vettel added: "It was quite close. I tried everything I could but unfortunately it wasn't enough.
"We are much closer to them here than in Silverstone [last weekend] so we are quite confident for tomorrow."
Rosberg was knocked out in second qualifying after Mercedes made the error of not sending him out for a second run at the end of the session.
The German, who has won two of the last three races, was knocked out of the top 10 as other drivers improved their times.
He dropped from being second behind Hamilton to 11th in the last four minutes of the session as he sat in the garage.
The man who knocked him out was Raikkonen, who moved up to second after himself dropping out of the top 10 in the closing minutes.
"It was quite a shock really," Rosberg said. "I didn't see it coming. The team didn't expect the track to ramp up that much. It really is a big disappointment. Unbelievable. We just didn't judge it correctly."
Mercedes' error was a boost for Button, who sneaked into the top 10 with a lap 0.664secs quicker than team-mate Sergio Perez, who qualified 13th.
On the occasion of the team's 600th grand prix, both Williams cars failed to progress beyond the first knock-out part of qualifying and will line up 17th and 18th, Valtteri Bottas 0.014secs faster than Pastor Maldonado.
Caterham's Charles Pic beat Marussia's Jules Bianchi by 0.126secs to win the battle of the backmarker teams.
Marussia's English driver Max Chilton was 1.035secs off the pace of his French team-mate.
Ahead of this weekend, the Grand Prix Drivers' Association (GPDA), of which all but three of the drivers are members, said they would "immediately withdraw" from the race meeting if the problems were repeated.
Vettel said that in the race the drivers would leave any decision on safety to race director Charlie Whiting, who can hear their radio transmissions to their teams.