Picture: David Mdzinarishvili / Reuters.

Baku - Lewis Hamilton surpassed Ayrton Senna's career haul of 65 pole positions in Formula One as he edged Mercedes team-mate Valtteri Bottas in a final lap shootout to secure top spot for Sunday's Azerbaijan Grand Prix.

Hamilton banished memories of a dismal qualifying session in Baku last year, when he clipped the barrier and had to settle for 10th place on the starting grid, to grab pole with a time of 1:40.593 despite running wide at Turn 16.

He will be joined in an all-Mercedes front row by Bottas, who was pipped at the death when Hamilton outpaced him in the final lap after the session had been paused by a red flag.

"That was one of the most exciting laps that I've had all year," he told Sky Sports. "A lot of pressure.

"The first lap I had the time but I made a mistake," he added.

"(There was) a lot of pressure because of getting temperature in the tyre but the lap just got better and better. Coming out of the last corner I was just 'thinking please be enough'."

The British driver is now just two behind Michael Schumacher's overall record of 68 poles in Formula One.

"That's how qualifying should be," Hamilton added. "Tomorrow will be a long hard race but we're in the best position to start."

Sebastian Vettel had his championship lead cut to 12 points when Hamilton won in Canada earlier this month, and now faces an uphill struggle to fend off his rival again after qualifying fourth.

"The result is okay but I'm not entirely happy," the four-times Formula One champion said. "This morning didn't help but it's not an excuse.

"I'm no fan of excuses."

Vettel was beaten to third by team-mate Kimi Raikkonen in an all-Ferrari second row.

Max Verstappen dominated Friday practice but Red Bull failed to have the anticipated impact as he qualified fifth and his team mate Daniel Ricciardo was down in 10th after hitting a wall, prompting the red flag which led to the last-lap shootout.

"It's not really nice to be in fifth," Verstappen conceded. "In Q3 on my final lap, where it all counts, I was two tenths faster but then had a problem with my gear sync, so on the straights I was losing a lot of lap time.

"That was very painful," he added. "We need to get on top of that because it shouldn't happen."

Canadian youngster Lance Stroll, in eighth, out-qualified his fellow Williams driver Felipe Massa, who was ninth. Force India duo Sergio Perez and Esteban Ocon finished sixth and seventh respectively.

Fernando Alonso, already hit with a 40-point grid penalty, failed to advance past Q1 for the first time this season, while Jolyon Palmer did not take part in qualifying after a fire in his car during Saturday morning's practice.

STARTING GRID

1. Lewis Hamilton (Britain) Mercedes

2. Valtteri Bottas (Finland) Mercedes

3. Kimi Raikkonen (Finland) Ferrari

4. Sebastian Vettel (Germany) Ferrari

5. Max Verstappen (Netherlands) Red Bull - TAG Heuer

6. Sergio Perez (Mexico) Force India - Mercedes

7. Esteban Ocon (France) Force India - Mercedes

8. Lance Stroll (Canada) Williams-Mercedes

9. Felipe Massa (Brazil) Williams-Mercedes

10. Daniel Ricciardo (Australia) Red Bull - TAG Heuer

11. Daniil Kvyat (Russia) Toro Rosso - Renault

12. Kevin Magnussen (Denmark) Haas - Ferrari

13. Nico Huelkenberg (Germany) Renault

14. Pascal Wehrlein (Germany) Sauber - Ferrari

15. Carlos Sainz Jr (Spain) Toro Rosso - Renault

16. Romain Grosjean (France) Haas - Ferrari

17. Marcus Ericsson (Sweden) Sauber - Ferrari

18. Stoffel Vandoorne (Belgium) McLaren

19. Fernando Alonso (Spain) McLaren

20. Jolyon Palmer (Britain) Renault