Lewis Hamilton took pole position in an enthralling wet qualifying session for the Hungarian Grand Prix. The rain gave Mercedes the chance to upend the form book after Ferrari appeared set for pole in the dry.

Hamilton headed team-mate Valtteri Bottas by 0.260 seconds, while title rival Sebastian Vettel could manage only fourth, behind Ferrari team-mate Kimi Raikkonen.

Hamilton leads Vettel by 17 points in the championship heading into the race. Ferrari dominated the earlier practice sessions, as the Mercedes drivers struggled with a lack of rear grip in hot, dry conditions, with both spinning at the chicane in final practice.

But a rain storm before qualifying doused the track and Hamilton was able to take advantage and what could be a crucial pole on a track where overtaking is notoriously difficult.

Hamilton said: “The Ferraris have been quicker all weekend and we were just trying to be close to them, but the heavens opened.

“It is so tricky out there. It was dry for a part of the lap and then wet. It is really difficult to arrive in the corner and know what grip you have.

Then at the end you were just looking for a grippy line.

“It is about give and take, each corner, I was up, I was down, I was up, I was down. It was an emotional rollercoaster.

“Were going to do our best [in the race] to work as a team and keep the red guys behind us.