Vettel, the defending Formula One world champion, dealt with a faulty kinetic energy recovery system during the final 20 laps at Barcelona.
His team decided not to jettison the KERS -- a device that recovers the kinetic energy present in the waste heat created by the car's braking process. That allowed Hamilton to gain on the Red Bull Racing driver.
But Vettel's KERS eventually kicked in, allowing him to boost acceleration and cross the finish line 0.6 seconds ahead of McLaren's Hamilton on the 2.892-mile Circuit de Catalunya.
The victory gave Vettel his fourth win in five Grand Prix events so far this year. He also improved his world championship lead to 41 points over Hamilton.
McLaren's Jenson Button rallied to finish third, making just three pit stops, while Red Bull's Mark Webber took the fourth spot.
Ferrari's Fernando Alonso wound up finishing one lap behind in fifth.