The 33-year-old Briton, who had struggled for outright pace, managed to steer his Mercedes through a chaotic race and capitalize on others’ misfortunes to register his first victory in seven races and the 63rd of his career.
Sebastian Vettel of Ferrari looked set to turn pole position into a victory until a second Safety Car intervention left him lunging to pass Valtteri Bottas and locked up and ran off.
Bottas then seemed sure to win, but suffered a sudden right rear puncture on his Mercedes after running over metal debris on the straight. Hamilton flew past to return to the top of the podium
“That was quite an emotional race, to be honest,” said hamilton, who now leads the championship by four points from Vettel.
“Valtteri deserved to have the win and also Sebastian did a great job. I was very fortunate today so it feels a bit odd to me to be here.”
Vettel managed to continue and finish fourth ahead of his teammate Kimi Raikkonen and Mexican Sergio Perez of Force India.
Carlos Sainz finishing fifth for Renault and Charles Leclerc sixth for Sauber, his first points finish.
Fernando Alonso finished seventh for McLaren ahead of Canadian Lance Stroll of Williams, Stoffel Vandoorne in the second McLaren and New Zealand’s Brendon Hartley of Toro Rosso who scored his first point in F1.
The race was heavily punctuated by accidents which included one in which the two Red Bulls of Daniel Ricciardo and Max Verstappen collided while fighting for fourth place in the closing stages, the pair having banged wheels and battled for positions throughout the race.
They ended up among the seven retirements on a challenging day on the second-longest circuit on the calendar.
In cooler conditions with a strong blustery wind, the drama unfolded from the start. As the lights went out, Vettel was away fast enough from his 53rd pole to hold off Hamilton in second, but behind them mayhem erupted.
Raikkonen, starting sixth, attempted to pass inside Ocon’s Force India, but the Frenchman turned in and as the pair collided he crashed into the wall.
In the surrounding chaos, Sergey Sirotkin was also contacted and he went down an escape road while Raikkonen limped back to the Ferrari pits. The luckless Alonso suffered punctured right front and rear tyres.
The multiple incidents required a Safety Car before the action settled and racing resumed, after five laps.
Vettel pulled away to open a three seconds lead ahead of Hamilton and Bottas after 10 of the scheduled 51 laps of the tight and fast Baku street track.
On lap 11, Nico Hulkenberg lost control of his Renault and clipped a wall at Turn Four as Ricciardo began a series of attacks on his Red Bull team-mate, the pair banging wheels to the consternation of their pit wall.
At the front, Vettel was in control and opened up a 3.6 seconds lead despite Hamilton recording fastest laps.
“These tyres are done,” complained Hamilton, who pitted to switch to ‘softs’ after 22 laps, rejoining third behind Bottas, but ahead of Verstappen. Vettel led the Finn by 12.5 seconds after 25 laps.
The Red Bulls continued providing thrills as they fought for fourth, Ricciardo passing Verstappen outside Turn One before the Dutch tyro regained the place inside Turn Two.
With 20 laps to go, Vettel finally pitted and passed the lead to Bottas, but rejoined 8.5 seconds ahead of Hamilton.
A frustrated Hamilton complained about back markers, but began clawing back time, recorded a fastest lap and then saw Bottas go eight-tenths quicker.
A tense and high-speed conclusion was clearly in prospect before, after their pit-stops had reversed positions, Ricciardo rammed into Verstappen under braking for Turn One on lap 41.
The Safety Car was deployed again and the leaders pitted for new ultra-soft tyres to set up a charging 10-lap finale.
The action resumed with four laps remaining and promptly delivered even more twists as Vettel lunged to take the lead from Bottas, but locked up and fell to fourth.
Then, with two laps remaining, Bottas suffered a right rear punctured after running over debris.
“Oh my God, it just blew up on me,” he said as Hamilton took the lead ahead of Raikkonen and Perez.