With five laps remaining the Red Bull driver’s luck ran out as his left-rear tyre punctured and he crashed into the concrete wall.
Verstappen was unable to contain his frustration as he got out of his car and kicked the blown tire.
The race was then suspended to clear debris strewn across the track.
The suspension offered Hamilton the chance to potentially win the grand prix or at least finish second and pull ahead of Verstappen in the drivers’ title race, but seeking to jump Perez at the restart the Briton ran off the track as he locked up.
Perez sped away to secure his first victory for Red Bull and only the second of his career.
Sebastian Vettel claimed second to give Aston Martin its first podium in F1, while Pierre Gasly finished third in the Alpha Tauri.
“I’m so happy for today,” said Perez after his victory. “Normally Baku is pretty crazy but I have to say sorry to Max.
“He deserved the win today and it would have been good for the team to get a one-two but the win is a fantastic result.”
Hamiiton described his conclusion to the race as “quite a humbling experience.”
“We’ve worked so hard to come back into the top 10 and I put everything on the line but just lost out in the end,” Hamilton told Sky Sports.
“I clipped a switch that places the brakes off so I just went straight on at turn one on the restart. I’m very sorry to the guys and girls in the team. I had no idea that I’ve even touched the ‘magic’ switch.”
Verstappen leads the drivers’ standing with 105 points, with Hamilton on 101 and Perez third on 69.
“We were fully in control — it’s very disappointing with what happened,” Verstappen told Sky Sports as he reflected on his crash. “We lost out on a lot of points — we could have opened up a gap, and with it happening so close to the end it is very frustrating.
“It is what it is — we’re still leading the championship despite hitting the wall.”
With six races completed, the next grand prix is in France on June 20.