Hamilton struggled to keep his tyres alive long enough in the early stages of the race to deal himself into a victory fight, but a safety car triggered by Daniel Ricciardo and Max Verstappen crashing into each other on lap 40 set off a series of events that cleared the Briton’s path to the top step of the podium.
Vettel, running in second behind Bottas, attempted to regain the lead on the inside of turn one, but he locked up and ran wide, dropping four places in the process.
Bottas should have cruised to the flag from there, but a high-speed puncture on the main straight forced him out of the race, and Hamilton inherited the lead in his place.
“Really quite an emotional race, to be honest,” Hamilton said. “Valtteri did such an exceptional job today and really deserved to have the win, and Sebastian did a really great job.
“It feels odd to be up here, but I’ve got to take it. Definitely an untidy race for me.”
The race had a frenetic feel right from the start, and a safety car was required on the first lap.
Sergey Sirotkin triggered contact with Nico Hulkenberg and Fernando Alonso – the stewards would later penalise him three grid places for the next race – on the run to turn three, spreading carbon fibre across the track, while further up the road Kimi Raikkonen and Esteban Ocon came together when the Finn attempted to cut under the Frenchman at turn four.
Ocon and Sirotkin were eliminated on the spot, but Alonso and Raikkonen limped back to the pits for repairs and new tyres.
Vettel perfectly judged the restart from Hamilton and Bottas in second and third, but behind the frontrunners the Renault and Red Bull Racing drivers were battling for fourth place.
Carlos Sainz led Nico Hulkenberg past Daniel Ricciardo and Max Verstappen by lap 10, but Hulkenberg undid his efforts one lap later when he crashed into the outside barrier at turn four and retired.
Now on their own, Ricciardo and Verstappen began sparring – Daniel had fallen behind Max at the restart and was desperate to reclaim the position. The pair dangerously banged wheels on lap 12 as the Australian attempted to pass around the outside of turn one, after which their tempers cooled for a time.
The race continued apace until lap 22, when Hamilton made his first pit stop, changing to the soft compound. Vettel followed suit on lap 30, but Mercedes left Bottas out, gambling that his worst result would be third but that a safety car would win him a free pit stop and keep him in the lead.
Luckily for him, Ricciardo and Verstappen had begun fighting for position again after making their sole pit stops for the ultrasoft compound.
On lap 40 Ricciardo slipstreamed behind his teammate into turn one, but as Verstappen moved into a defensive position in the braking zone, the Australian ran out of room to dive down the inside.
Ricciardo hit the back of Verstappen’s car, sending both careering into the run-off area and out of the race.
Bottas pit under the safety car, as did the rest of the field, and emerged with the lead ahead of Vettel and Hamilton.
The Finn needed only to hold position at the restart to win the race, and when Vettel locked up at the first turn in a botched assault on the lead, it seemed he was destined to do so.
But Azerbaijan had one last twist in store: a puncture one lap after the resumption sent him out of the race and handed victory to his teammate.
Kimi Raikkonen in the second Ferrari followed Hamilton home to take an unlikely second place after getting caught up in the first-lap melee, but the stand-out podium-getter was Force India’s Sergio Perez.
“I think today I did the best two laps of my whole career,” said the Mexican of his final laps after the safety car. “The last laps with Sebastian behind on cold tires, it was so difficult.
“I tried to keep close to Raikkonen to make sure Sebastian didn’t get close enough, and in the end we did it. I’m speechless.”
Vettel came home fourth ahead of Renault’s Carlos Sainz and an impressive Charles Leclerc for Sauber in sixth, the Monegasque driver scoring his debut F1 points.
Fernando Alonso salvaged seventh despite his heavy first-lap crash damage, holding at bay Lance Stroll, who scored Williams’s first points of the season.
Stoffel Vandoorne finished ninth in the sister McLaren, and Toro Rosso’s Brendon Hartley scored his debut F1 points in tenth.
Don’t forget to listen to Live89.5 each and every Saturday at 9am and 5pm for the Box of Neutrals radio show.