Bottas looked set to celebrate just the fourth race victory of his career before he drove over a large chunk of debris that had been left on the track following a high-speed crash between Red Bull teammates Daniel Ricciardo and Max Verstappen.
It forced the Finn out of the race and handed the lead to Hamilton with just three laps remaining. Ferrari's Sebastian Vettel started on pole and looked in good shape to record a third win of 2018 but he took himself out of podium contention after a failed overtake on Bottas.
Hamilton, the reigning world champion who started second on the grid behind Vettel, now leads his German rival by four points in the drivers' standings after four races of the season.
The defining moment of the race was the incident between Ricciardo -- the winner last time out in China -- and Verstappen, who had been jostling for position all race.
It finally came to a head on lap 40, when Ricciardo attempted to pass Verstappen down the inside but only succeeded in colliding with the back of the Dutchman's car, putting both drivers out of the race.
And it was debris from the collision that punctured Bottas' tire, much to the annoyance of Mercedes.
"Why didn't they clean up the circuit properly?" Mercedes non-executive chairman Niki Lauda told Sky Sports.
"There was so much time to do it ... Safety car was on for forever, why did nobody pick it up?"
When asked by a reporter how he'll get over the setback, Bottas replied: "I don't know, maybe 10 pints of beer?"
"Street circuits are difficult, there are always going to be a lot of crashes," he added. "I didn't know I had run over anything or seen anything. Just unlucky."
Bottas' misfortune gave Hamilton an unexpected victory as he bids to become just the third driver to win five world championships.
"Very much mixed emotions, one is grateful of the opportunity to win a grand prix and two that I didn't drive like I wanted to drive," said Hamilton. "I have got some work to do, but I can go home on a high note.
"I was coming round the last corner in disbelief of the position I was in. It was very emotional for me. I have been in the lead here before and something happened.
"This is a track where luck is needed, there are a lot of safety cars and a lot of incidents."
Both Ricciardo and Verstappen insisted there were no hard feelings between them after a crash that had looked likely from the first lap of the race.
"We are thankful we are allowed to race, myself and Max love to race as we show," said Ricciardo. "We did get close a few times, sometimes we are on the limit and unfortunately it ended like it did. It is not a nice situation and we both feel bad.
"As far as the incident itself, it is a tough one. I thought the gap was opening on the outside and once I'd come out I had to commit."
"As racing drivers you go for every inch," said Verstappen. "We are very fair to each other and spoke immediately afterwards."
Ferrari's Kimi Raikonnen finished second, with Sergio Perez taking a surprise third place for Force India.