Hamilton’s championship rival Sebastian Vettel was expected to be the fastest in the dry, but, as ever, Hamilton turned on the style when the heavens opened.
The Englishman beat team-mate Valtteri Bottas by a quarter of a second to the top slot on the grid, while Ferrari’s Kimi Raikkonen third.
Vettel, 17 points behind Hamilton in the championship, finished only fourth, more than half-a-second down on his rival.
Dark clouds hovered over the Hungaroring before large claps of thunder ensured a downpour was imminent.
Moments later, the rain arrived to ensure the first wet qualifying session since last September’s Italian Grand Prix.
On that day, Hamilton prevailed with a remarkable lap, and here the Briton was at it again to prove his credentials in the slippery conditions.
Vettel, who crashed out from the lead of his home race in Germany last week, appeared cautious, and will now have his work cut out from fourth at a track where overtaking is difficult.
“We couldn’t have expected this because Ferrari have been quickest all weekend,” Hamilton, only fourth in final practice, said. “We were going to try and do our best to be as close to them as possible, but then the heavens opened and it was fair game.
“It is so tricky out there. Towards the end it was getting really wet and it is difficult to arrive at the corner and know how much grip you are going to have.
“That was massively challenging. You are tip-toeing around like a ballerina. We are in a great position so we are going to do our best to keep the red guys behind us.”
Red Bull driver Daniel Ricciardo was the biggest victim to fall foul to the changeable conditions.
The Australian failed to get in a good enough lap at the start of Q2, and as the rain intensified, he was unable to haul his car into the top-10 shootout. He qualified only 12th.
Fernando Alonso was on hand to state just how wet the track had become when asked by his race engineer if he should stick to intermediate tyres or change to the full wet rubber.
“We should go to the garage,” he said. “Put on whatever you like. The last sector is impossible. Even if you put on a rocket ship we will [fail to improve on] 11th.”
And that is where the double world champion, who on Sunday turns 37, will start.
His team-mate Stoffel Vandoorne, who has been desperately out of sorts in recent races, was 16th.
Cash-strapped Force India were placed in administration on Friday evening following a court hearing, and their problems away from the track were replicated on it here.
Esteban Ocon and Sergio Perez, the Mexican driver who initiated the legal proceedings with his sponsors owed more than £3million, fell at the first hurdle and will start only 18th and 19th of the 20 runners.
Vettel is bidding to bounce back from his nightmare race in Germany after he crashed out from the lead to hand the championship advantage back to Hamilton.
Ferrari arrived here hoping that the slow-speed Hungaroring circuit would suit their machinery, and so it proved as Vettel saw off the Red Bull duo of Max Verstappen and Daniel Ricciardo to claim top spot.
Verstappen, however, was just seven hundredths of a second down on Vettel to suggest that Red Bull could take the fight to Ferrari this weekend.
As for Hamilton and Mercedes, they appear to have their work cut out at the sport’s final round before the month-long summer shutdown.
Hamilton, who holds a 17-point lead over Vettel, was fifth in both practice sessions, and finished an alarming three quarters of a second behind Vettel in the afternoon running.
His Mercedes team-mate Valtteri Bottas was even further back in sixth with the Silver Arrows, as has been the case in recent seasons, off the pace at this slowest of the non-street circuits.
Traditionally, Ferrari also tend to hold a little in reserve during practice, so Vettel’s speed is likely to be a concern to their rivals.
Fernando Alonso’s McLaren team have brought a heavily-revised car to Hungary, but there was little improvement for the Spaniard.
Alonso, who celebrates his 37th birthday on Sunday, was only 12th in the order, two seconds down on Vettel.
His team-mate Stoffel Vandoorne has been desperately out of sorts in recent races, and the Belgian was second-to-last in the standings, nearly a full second behind Alonso. He was also fortunate to avoid crashing into the barriers after spinning off at turn five.
Elsewhere, Romain Grosjean was the best of the rest as he finished seventh for Haas. Carlos Sainz was eighth for Renault ahead of Toro Rosso’s Pierre Gasly. Esteban Ocon completed the top 10 in his Force India.
Hamilton is 17 points clear of his rival ahead of the sport’s final round before the summer break in Hungary on Sunday.
To date, the Englishman, 33, holds the mental edge over Vettel following the Ferrari driver’s fourth, and biggest mistake of the year, when he crashed out from the lead in Germany last Sunday.
Hamilton’s remarkable victory in Vettel’s back yard moved him to the summit of the standings to mark the fifth change of championship leader this season.
For the first time in the hybrid era, Ferrari, who are sporting a black stripe on their cars this weekend in memory of chairman Sergio Marchionne who died suddenly on Wednesday, boasts the strongest package.
That was again on show in practice here as Vettel topped the time sheets with Hamilton only a distant fifth.
By his own admission, the Briton has not been at his very best this year, but it is his lack of mistakes, and flashes of once-in-a-generation brilliance, that ensure he remains the favourite to clinch a fifth world crown.
“The psychological game in sport is the hardest thing,” Hamilton said. “You don’t see me away from the track. I wake up with insecurities.
“The most demanding thing is keeping your mind in the game from the first race in March right through to the November, and arriving at every race 100 per cent.
“I haven’t hit the nail on the head at every weekend this year, but the pressures are huge, and the demand on myself and Sebastian is higher than ever.
“It has swung more in Ferrari’s direction so I am having to over-deliver. The pressure to extract every ounce is greater than ever if I want to be number one at the end.
“That is not something I am fazed by, but something that excites me. I have always felt that I have been at my best under pressure. I welcome it.”
Vettel, but for his mistakes, should be leading this championship. He dropped from second to fourth after running off the road in Azerbaijan.
He collided with Valtteri Bottas in France, and was demoted five places on the grid at the ensuing race in Austria after blocking another driver in qualifying.
Last weekend, with Hamilton catching him in the slippery conditions, Vettel threw away a certain 25 points.
“It is closer than last year, so the smallest mistakes are even more costly,” Hamilton added. “But I take pride in being a perfectionist and not making mistakes.
“Nobody is perfect, but I work to position myself mentally and physically so I am the last to crack.”
There have, however, been signs of the intensity getting to Hamilton. He wildly accused Ferrari of playing dirty at Silverstone (Kimi Raikkonen collided with him on the first lap), while his reaction after his car broke down in qualifying last weekend was as if he had lost the title.
In a deleted Instagram post, he then said he did not get the credit he deserved from Sky Sports for his comeback fight from 14th to first last Sunday.
“Positive headlines don’t sell newspapers, and don’t generate clicks,” Mercedes boss Toto Wolff said. “In the here and now the great achievements are not recognised how they should be.
“In five or 10 years, we will look back and say we were part of an amazing driver’s journey. He wears his heart on his sleeve, and polarises opinion, but for me that is fine.”