Having run behind Vettel in the early stages, Hamilton’s hopes of victory on the Baku City Circuit looked all but over when he suffered a double-front lock-up in the opening stint that forced him to stop first among the frontrunners.
But following a late Safety Car intervention, triggered when the Red Bulls collided spectacularly, Hamilton's luck turned around dramatically.
First Vettel, who had dropped to second following his pit stop, outbraked himself attacking Vallteri Bottas for the lead into Turn 1 allowing Hamilton into second. Then, with just two laps to go, Bottas suffered a heartbreaking exit after a spectacular debris-induced tyre blow out.
That left Hamilton in the clear to snap a six-race winless run - his longest in the turbo era - though he admitted he didn't feel he necessarily deserved to be on the top step of the podium.
“It was a really quite an emotional race,” said the Briton, who extended his record point-scoring streak to 29. “Valtteri did such an exceptional job and deserved to have the win, Sebastian did a great job.
“It was very fortunate today. It feels odd to be up here but I’ve got to take it. I didn’t give up, I kept pushing.
“To come out with a win, I feel incredibly grateful. You have to take the ups with the downs. I can’t complain that I won.
“Valtteri was unfortunate. I wouldn’t have got by him at the end. Straight away when I got out the car, I went back to see Valtteri to congratulate him on a great job, which is why I was late to the podium.”
Despite vaulting into the championship lead, Hamilton admitted he needs to improve as a driver, while Mercedes, who struggled for form all weekend in Baku, have work to do because he feels Ferrari are still the clear pace-setters.
“Ultimately Valtteri did an exceptional job,” he said. “I did a good job in qualifying but there were a lot of faults in the race. I struggled with the car, I struggled with the tyres.
“I’ve got to got to go away from here and work harder to ensure there is no repeat performance-wise from myself.
“Ferrari still have the upper hand. They have outperformed us most of the time. Our qualifying pace is not on par with theirs, we have some work to do.
“We have to keep our heads down. This weekend was a solid group effort. We were several steps behind, trying to catch up.
“When we got to the race, things started to come to us. I’m grateful for the team. While today wasn’t perfect, I hope we can still take some positives.”
When asked if the pressure had been released following Mercedes’ first victory of the season, their non-executive chairman Niki Lauda said: “For sure, a win is better than a loss.
“There’s no question about it. One lost, another won, so thank God.”
Having arrived in Baku with a nine-point deficit to Vettel in the standings, Hamilton now heads the Ferrari driver by four points, with Kimi Raikkonen 18 further back in third.