Sebastian Vettel claimed pole position for the German Grand Prix, while Formula 1 title rival Lewis Hamilton was only 15th after stopping with a gearbox problem in Q1.

The Ferrari driver held pole position after the first runs in Q3, with all drivers on ultrasofts, but faced a challenge from the remaining Mercedes of Valtteri Bottas on the second runs.

Bottas briefly claimed pole position with a lap of 1m11.416s, helped by a mighty run through the stadium section.

But Vettel, who was faster in the first two sectors before losing a quarter of a second to Bottas in the final sector, did enough to take pole by 0.204s on his final lap.

The second Ferrari of Kimi Raikkonen was third, 0.335s off Vettel, having again looked like a potential threat for pole position.

He made a mistake at Turn 12 on his first run that cost around three tenths after he hit the inside kerb, then couldn't quite find the pace on his second run.

Red Bull driver Max Verstappen was fourth, just under three tenths behind Raikkonen, and benefitted from the absence of Hamilton.

Hamilton was ordered to stop on his second Q1 run after losing gear selection, which followed immediately after he ran wide in Turn 1.

In rejoining, he struck the rumble strip at the exit of the corner, which kicked the car up and appeared to cause the problem that manifested itself on the run to Turn 2 - although he reportedly subsequently said the problem had appeared before this moment.

Hamilton attempted to get the car back to the pits but eventually stopped at Turn 10 after being ordered to do so by the team.

Currently 15th thanks to setting a time good enough to escape Q1 before the problem, he will move up a place thanks to Daniel Ricciardo's penalties – subject to incurring any grid drops himself.

Haas pairing Kevin Magnussen and Romain Grosjean locked out the third row with fifth and sixth, with the lead Renault of Nico Hulkenberg missing out on splitting the pair by 0.016s.

The second Renault of Carlos Sainz was eighth, ahead of the Sauber of Charles Leclerc and Sergio Perez's Force India.

Fernando Alonso was quickest of those eliminated in Q2 in 11th place, with a gap of six tenths to Perez ahead in that segment of qualifying.

That put him ahead of Williams driver Sergey Sirotkin, who posted the team's best qualifying result since April's Azerbaijan Grand Prix in 12th place.

Sauber driver Marcus Ericsson was 13th and slowest of those to set a time in Q2 after causing a nine-minute red flag period when he spun into the gravel at the Turn 13 left-hander.

Ericsson was able to dig himself out of the gravel, but in doing so pulled it onto the track and led to the session being stopped two minutes later.

After his second run, the Swede suggested that he lost grip on his final qualifying attempt and speculated he might have sustained some minor damage when he hit a kerb.

Esteban Ocon was bumped into the drop zone late in Q1 when Force India teammate Perez improved on his second push lap on his second set of ultrasofts.

Ocon went into qualifying with only one dry free practice session under his belt, having sat out FP1 to allow Nicholas Latifi to drive then been hit by rain in FP3.

Toro Rosso pair Pierre Gasly and Brendon Hartley were 17th and 18th, separated by three-tenths of a second.

Lance Stroll was 19th, while Stoffel Vandoorne's troubled weekend continued as he brought up the rear, two tenths slower than the Williams driver.

1 germany Sebastian Vettel  Ferrari Ferrari 1'11.212  
2 finland Valtteri Bottas  Mercedes Mercedes 1'11.416 0.204
3 finland Kimi Raikkonen  Ferrari Ferrari 1'11.547 0.335
4 netherlands Max Verstappen  Red Bull TAG 1'11.822 0.610
5 denmark Kevin Magnussen  Haas Ferrari 1'12.200 0.988
6 france Romain Grosjean  Haas Ferrari 1'12.544 1.332
7 germany Nico Hulkenberg  Renault Renault 1'12.560 1.348
8 spain Carlos Sainz   Renault Renault 1'12.692 1.480
9 monaco Charles Leclerc  Sauber Ferrari 1'12.717 1.505
10 mexico Sergio Perez  Force India Mercedes 1'12.774 1.562
11 spain Fernando Alonso  McLaren Renault 1'13.657 2.445
12 russia Sergey Sirotkin  Williams Mercedes 1'13.702 2.490
13 sweden Marcus Ericsson  Sauber Ferrari 1'13.736 2.524
14 united_kingdom Lewis Hamilton  Mercedes Mercedes    
15 france Esteban Ocon  Force India Mercedes 1'13.720 2.508
16 france Pierre Gasly  Toro Rosso Honda 1'13.749 2.537
17 new_zealand Brendon Hartley  Toro Rosso Honda 1'14.045 2.833
18 canada Lance Stroll  Williams Mercedes 1'14.206 2.994
19 belgium Stoffel Vandoorne  McLaren Renault 1'14.401 3.189
20 australia Daniel Ricciardo  Red Bull TAG

Gallery: German GP: Vettel beats Bottas to pole, drama for Hamilton