The 29-year-old German, who leads Hamilton by 14 points in their duel for the drivers' title, cashed in after the luckless Briton's car burst into flames in the opening minutes of a dramatic qualifying session.
Rosberg clocked a best lap of one minute and 22.715 seconds to claim his sixth pole of the year and the 10th of his career in an hour that saw intense heat, at the start, followed by dark clouds and rain in the final minutes.
That caused red flags and the session was halted for five minutes, resuming in damp, but drying conditions as the hot sunshine resumed - a scenario that saw a series of drivers in serious contention for pole.
Vettel clocked a best lap of 1:23.201 that put him clear at the top in the final seconds, but Rosberg responded to outpace him by 0.486 seconds to secure the prime starting position.
Also Read
Briton Jenson Button, twice a winner in Hungary, in wet conditions, was seventh for McLaren ahead of Frenchman Jean-Eric Vergne of Toro Rosso, German Nico Hulkenberg of Force India and Magnussen.
"We'll have to replace the engine and gearbox, but with that penalty it's a good thing (for a gearbox change)," said Hamilton. "I can't go any further back.
"There's a lot going through my mind, but I just have to try to turn it into positives until tomorrow. I think it's getting to the point beyond bad luck - it's something else. We just need to do better.