(CNN) Lewis Hamilton put in a scintillating drive to charge from behind and overtake Max Verstappen with just three laps remaining to claim his seventh Hungarian Grand Prix victory.

For the vast majority of the race, it looked as though the Red Bull driver would convert his first career pole position into an eighth race victory but a gamble from Mercedes with 20 laps remaining gave Hamilton a sniff of a possible win.

Having tried and failed to pass Verstappen for a number of laps, the Silver Arrows decided to bring its driver into the pits for a fresh set of tires.

Hamilton reemerged onto the track a little under 20 seconds behind the Dutchman; the only question was whether or not there were enough laps for him to catch his rival.

The team's calculations gave Hamilton a chance of overtaking Verstappen on the very last lap. In the end, he didn't need that long and stormed past his floundering rival with three laps remaining.

Lewis Hamilton and Max Verstappen embrace following their thrilling battle.

Hamilton's Mercedes teammate Valtteri Bottas struggled to eighth place, meaning the Brit extends his lead at the top of the drivers' championship to 62 points.

"Tired," was how Hamilton said he felt following that epic tussle. "Which is how it should be, but I feel really grateful for the day and really for the team for continuing to believe in me and continuing to push to the limits and take a risk and a chance on me.

"For a race to be able to push like that, I'm telling you I was on the limit all the way. It was very, very difficult to get by, [Verstappen's] defense was great. Honestly I didn't know if I could catch that 19-second gap.

"My tires were going drop off -- all these things going through your mind. But like the team said, just keep your head down so I just did and kept pushing and pushing and the gap closed and closed and closed.

"My hat off to the team and I think if Niki [Lauda] was here today he would take his hat off."

Good start, bad finish

Verstappen got off to a flying start, easing into a comfortable lead as the two Mercedes battled and slowed each other down behind him.

The 21-year-old must have been cheering internally watching the action unfold in his wing mirrors. Bottas locked up his brakes on each of the first two corners, allowing teammate Hamilton to pass him around the outside.

Perhaps the Finn is feeling the pressure of his seat at Mercedes reportedly being under threat for next season.

Rumors suggest that Bottas will know as early as the Belgian Grand Prix -- the first race back after the August summer break -- whether or not the team's reserve driver, Esteban Ocon, will replace him for 2020.

Things then went from bad to worse, as Bottas was forced to pit on lap six to change his front wing which sustained damage in minor collisions with Hamilton and Sebastian Vettel on the opening two corners.

When he reemerged from pits, he was dead last and almost a minute behind race leader Verstappen, who he had started next to on the grid just a few laps ago.

Far out in front of the congested field, Verstappen and Hamilton were pulling away and engaged in an intriguing battle.

Lewis Hamilton celebrates his seventh Hungarian Grand Prix win.

Red Bull decided to pull its driver in to pit first, before Mercedes called Hamilton in a few laps later -- though a mistake when changing the rear right tire cost the Brit valuable time.

When he came back onto the track, the gap to Verstappen was up to almost six seconds.

He was advised by his engineer over the radio to look after his new tires, but took matters into his own hands and was on Verstappen's tail within two laps.

What ensued was a thrilling, wheel-to-wheel battle over several corners as Hamilton tried to take the race lead. It was an enthralling tussle between arguably the two best drivers on the grid.

Roll the dice

Verstappen eventually forced his rival wide and off the track and emerged unscathed still leading the race.

The young driver may have won seven races already in his fledgling career but this was the first time he had ever qualified on pole.

Coming from behind to win is one thing, but attempting to lead from start to finish brings a whole new kind of pressure.

Lewis Hamilton extended his championship lead over Valtteri Bottas, who struggled to eighth place.

Verstappen's radio messages to his engineer were becoming increasingly more frantic, querying everything from his car's power to the team strategy.

With 20 laps remaining and Hamilton's attempts to pass beginning to wane, Mercedes took a gamble -- with nothing to lose in second place -- and decided to pit Hamilton for a second time and put on a set of new tires.

On fresher tires and able to put in faster lap times than Verstappen, Hamilton was soon bearing down ominously on the race leader.

With just six laps remaining, a skittish Verstappen lamented into his team radio: "The tires are dead," and so were his chances of a win.