Nico Rosberg grabbed pole position ahead of his world championship leading Mercedes team-mate Lewis Hamilton for Sunday's German Grand Prix, with a brilliant lap in the final minutes of a tense qualifying session on Saturday.

The 31-year-old German produced a startling best lap of one minute and 14.323 seconds to outpace the defending champion by 0.123 seconds, having been forced to abort his first flying lap in the third qualifying session due to electrical problems.

When Hamilton went for his final lap, he was unable to improve and regain the initiative.

Rosberg, on home soil, has set himself up for a chance to regain the lead in the drivers' standings after slipping six points behind three-time champion Hamilton when the Briton won in Hungary last week.

"Well done, Nico, a fantastic lap and well done under the circumstances," said Mercedes technical chief Paddy Lowe, who revealed that Rosberg's car was heavily fuelled.

It was Rosberg's third pole at Hockenheim, 30 years after his father Keke had claimed his final pole at the famous old circuit, his fifth of the season and the 27th of his career.

Behind the two Mercedes, Australian Daniel Ricciardo, in his 100th Grand Prix, was third fastest for Red Bull ahead of his team-mate Max Verstappen, the pair slightly more than three-tenths behind.

The two Ferraris of four-time champion Sebastian Vettel and Finn Kimi Raikkonen were fifth and sixth ahead of Nico Hulkenberg of Force India, Valtteri Bottas of Williams, Sergio Perez in the second Force India and Felipe Massa in the second Williams.

"Yes it was a great feeling," said Rosberg. "It was a great lap, but I also had extra fuel just in case I needed to go again and do a third lap."

Hamilton lost time on his final lap when he locked up at the hairpin, while Rosberg revelled in the technical demands of the track and obeyed the adage of 'making the car do the work'.

- Topsy-turvy battle -

Hamilton topped the first-session times with a best lap of one minute and 15.243 seconds to outpace Rosberg by two-tenths, ending the German's supremacy.

Briton Jolyon Palmer squeezed through with a late effort in his Renault and out went Kevin Magnussen, his team-mate, along with German Pascal Wehlrein, luckless Russian Daniil Kvyat, Indonesian Rio Haryanto in the second Manor and the two Saubers of Felipe Nasr and Marcus Ericsson.

A despondent Kvyat was apoplectic after his disastrous session.

"Unbelievable," he said, when told he was out again. "What the fuck is going on?"

In the second session, the Mercedes duo switched from softs to super-softs and after Rosberg began with 1:14.839, Hamilton scorched round in 1:14.748.

Content with their first flying laps, the top contenders stayed in the pits as the rest scrambled to reach the top ten.

In the end, it spelt elimination for Mexican Esteban Gutierrez along with Jenson Button of McLaren and Spaniards Carlos Sainz and Fernando Alonso in the second McLaren.

Frenchman Romain Grosjean also went out in 15th place in the second Haas, knowing he faced a grid penalty for a change of gearbox, and Palmer.

This meant that the top ten shootout included both Mercedes, both Red Bulls, the two Ferraris – though Vettel only went through in ninth – and both Williams and Force India cars, all four of them powered by Mercedes.

Vettel set the early standard with 1:15.619 before the Silver Arrows did their stuff, Rosberg lapping fast, but then pulling into the pits as Hamilton clocked a best lap in 1:14.486.

Rosberg was under pressure, as his team said he had suffered an electrical error.

That gave Verstappen and then Ricciardo encouragement and they followed each other into second place before they all came back out for their final runs.

Rosberg was soon out again on a virtually clear track and retook the lead with his flying lap, outpacing his team-mate's first run by 0.123 seconds.

Hamilton came out for another try, but he was unable to outpace a team-mate at Hockenheim for the first time since 2008.