Hamilton bagged his third consecutive pole and fourth of the season with a best lap of 1min 19.388sec with Red Bull’s Vettel timing 1.19.426 for a margin of 0.038sec.
Frenchman Romain Grosjean in a Lotus was third fastest to take his place on the second row of the grid.
It was 2008 champion Hamilton’s fourth pole at the Hungaroring circuit.
The 28-year-old Briton pulled off his fastest lap right at the conclusion of a closely-fought session, having spent Friday battling handling and balance problems with the new Pirelli tyres.
His rivals had been able to test the tyres at Silverstone, where Mercedes were banned from running last week.
Hamilton admitted: “With the temperatures, it was pretty tough. I was really surprised when I came across the line and they said I got pole. I didn’t feel like that was a great lap.
“I thought Sebastian had got it. I’m really surprised, I thought maybe he made a mistake or something.
“We brought some upgrades this weekend, the guys are working hard and it’s a result of all hard work they’ve put in.
“Starting on pole definitely helps as it’s difficult to follow here. It’s a long way down to Turn One, but if you can get there first and try and hold position, it’s definitely advantageous.
“But these guys (Vettel and Grosjean) are going to be ridiculously fast so trying to keep in front or holding onto them is going to be the challenge.”
Hamilton’s latest gambit in temperatures hitting 35 degrees Celsius at the start with track temperature reaching 50 degrees lifted him beyond five-time champion Argentine Juan Manuel Fangio to a career total of 30 poles.
After squeezing defending triple world champion Vettel out Hamilton had on his slowing down lap asked his team: “”Are we on pole?” The response from his care engineer was: “Yeah – and we are as surprised as you, mate!” Team chief Ross Brawn said simply, “brilliant job, Lewis.”
Vettel praised Hamilton.
“Lewis did an exceptional job today – you have to hand it to him.”
But the German, seeking a first career win in Hungary, stressed: “I am still in a good position for tomorrow and we have a good car for the race.
“I was pretty happy with both laps because I had two sets of new tyres, but maybe I wasn’t aggressive enough in the middle.”
Grosjean said: “I like this circuit, but that’s not important. It’s been a good weekend, we’ve been working hard with the team trying to get the best out of the car and the last lap was pretty good.
“The race is very long and it’ll be very hot, but we will see what we can do to get the best result.”
German Nico Rosberg in the second Mercedes was fourth fastest ahead of two-time champion Fernando Alonso of Ferrari, who outpaced 2007 champion Kimi Raikkonen in the second Lotus, Felipe Massa in the second Ferrari and Daniel Ricciardo of Toro Rosso.
Mexican Sergio Perez recovered from a crash in morning practice to take ninth place for McLaren ahead of Mark Webber who sat out the session for Red Bull after yet another KERS failure.
Briton Jenson Button, the 2009 champion, qualified 13th.