The championship-leading German produced a fastest lap of one minute and 19.965 seconds at the end of a session stretched to nearly two hours by heavy rain and crashes in the hazardous conditions on the twisting Hungaroring circuit on Saturday.
Defending three-time champion Hamilton, who is just one point behind Rosberg after 10 of this year's 21 races, was 0.143 seconds behind his rival as the pair secured their team's sixth front-row lockout of the season.
"It was a really challenging qualifying with the changing conditions all the time," said Rosberg, 31, who six hours later had to survive a meeting with the stewards over his pole lap before he was cleared of any wrongdoing.
"I've been feeling really good out there the whole weekend in all conditions. I'm looking forward to the race and the great opportunity tomorrow."
Hamilton looked to be on course to take pole when on an improved best lap he came across Spaniard Fernando Alonso's McLaren spinning off the track dangerously in front of him.
The Briton had to take evasive action, which cost him time, before Rosberg came round and was able to continue without easing off.
"It was unfortunate with Fernando, but these things happen," said a stony-faced Hamilton, who has been gnawing away at Rosberg's lead in the standings and is chasing a third win in succession.
"I did the best I could do and it's a long race tomorrow so I'll give it everything I've got."
The two Red Bulls of Daniel Ricciardo, who was also affected by Alonso's spin, and Dutch teenager Max Verstappen were third and fourth ahead of four-time champion Sebastian Vettel of Ferrari.
Vettel's team-mate Kimi Raikkonen was unable to progress from Q2 in the treacherous conditions.
Red flags abound
Spaniards Carlos Sainz of Toro Rosso and Alonso were sixth and seventh ahead of Briton Jenson Button in the second McLaren, Nico Hulkenberg of Force India and Valtteri Bottas of Williams, who was 10th fastest.
Rosberg's success brought him his 26th career pole position and his 50th front-row start for a race he has never won.
Heavy rain delayed the start of Q1 by 20 minutes, flooding many parts of the paddock, including television commentary booths.
"It's pretty hairy out there, there's a lot of spray and standing water," said Williams deputy team chief Claire Williams.
"The most important thing is the safety of our drivers."
When it did finally get going, there were four red flags in Q1 alone, as the drivers struggled badly.
One of the victims was Swede Marcus Ericsson in his Sauber, slamming into advertising boards in front of a tyre barrier at Turn Ten, and bring another red flag with nine minutes remaining.
Then his team-mate Felipe Nasr crashed. He was unhurt, but like Ericsson, his day was over.
Another red flag, the fourth, with five minutes to go ensured a thrilling finale to Q1.