Hamilton crossed the line at the Hungaroring just 0.003 seconds ahead of Red Bull rival Max Verstappen to take his first pole since the penultimate round of the 2021 season in Saudi Arabia.
His lap was greeted with raucous cheers from the crowd, bringing to an end a run of five straight poles for Verstappen. Lando Norris qualified an impressive third for McLaren, and will start alongside his team-mate Oscar Piastri on the second row.
“It’s been a crazy year and a half,” Hamilton said. “I’ve lost my voice from shouting in the car.
“I’m so grateful for that feeling because the team have worked so hard, pushing so hard, so to finally get the pole, it feels like the first time.
“Big, big thanks to the crowd. We have amazing crowds every year here in Budapest. I didn’t expect coming here today we would be fighting for pole. When I went into the last run I gave it absolutely everything. There was nothing left in it.”
Hamilton only finished 16th on Friday, describing his machine as “at its worst”.
But the 38-year-old, who has won more times here than anybody else and captured his first victory in Mercedes colours at this venue a decade ago, will believe he can end the longest losing streak of his career in Sunday’s 70-lap race after outclassing Verstappen and claiming his ninth pole at this track.
After leaving his cockpit, the seven-time world champion stood on the front-right tyre of his Mercedes before saluting the main grandstand and then affording Verstappen a cursory handshake.
Norris finished less than a tenth back as McLaren’s resurgence continued, with team-mate Piastri fourth.
Mercedes have looked strong at this slow and twisty venue, but Hamilton’s team-mate George Russell will start only 18th following a qualifying run to forget for the British driver.
Russell qualified on pole last year, but he will start two from the back after a session the Briton called a “s***show”. He attempted to create space ahead of his final lap, but in doing so, he allowed three of his competitors through at the final corner.
It meant the 25-year-old was well off the pace in the first sector, and he was then unable to claw the deficit back over the remainder of the lap.
“It is a s***show,” he yelled over the radio. “What was that? What was that? Don’t tell me we’re out.”
When his race engineer Marcus Dudley confirmed the bad news, Russell replied: “F****** hell.”
The television cameras then cut to the rear of the Mercedes garage with an apoplectic Toto Wolff shaking his head before he slammed his right fist on the table in front of him.
“We were fast, the car felt great, but for the whole session we were out of sync with everybody,” Russell told Sky Sports. “I was three tenths down before I got to Turn 1, the tyres were nowhere, and it was totally normal that we were slower and didn’t make it through.
“I am really disappointed. We didn’t need to take so many risks. It’s rare we make these mistakes. You get what you deserve if you don’t do things right.”
As Russell was dealt an early bath, the returning Ricciardo snuk through to Q2 by just 0.011 sec. He is back in the saddle after seven months on the sidelines but on his debut for AlphaTauri, the Australian out-qualified team-mate Yuki Tsunoda. He will line up a respectable 13th on the grid, four places ahead of Tsunoda.