(CNN)Lucas di Grassi won a thrilling Mexico E Prix, pipping race leader Pascal Wehrlein as his battery died on the line.
Mahindra's Wehrlein led for large parts of the race but his battery's charge hit 0% just meters from the end, allowing Di Grassi to pass him on the inside.
The German driver was eventually demoted to sixth place after a five-second penalty for cutting off a chicane, as Antonio Felix da Costa and Edoardo Mortara came second and third respectively.
"I pretended to go to the outside and he opened a small door, just enough for the car to fit between the wall and the car and I went there," a shirtless and champagne-soaked Di Grassi told reporters of his last-gasp overtake.
"I couldn't believe it. My adrenaline is still super high. It was amazing. I knew he was running out of energy so I was putting pressure to overtake in the last five lap.
"This was probably the best Formula E race of my career because it was a mixture of having the patience to save energy in the beginning and then making the moves in the correct moment."
Jerome D'Ambrosio, Wehrlein's Mahindra teammate, retained his overall championship lead thanks to his fourth place finish in Mexico.
Officials were forced to red flag the race for long periods after Nelson Piquet Jr. smashed into the barriers following a huge collision with Jean Eric Vergne.
It left the Brazilian out of the race and neither driver was willing to take responsibility for the incident.
"JEV seemed like he was saving energy a lot by that point of the race already," Piquet Jr told Autosport.
"I even thought he had a problem so I went to the inside, he started closing the door and I was ready to really commit to the corner and brake on the inside and pass."
"When it happened, straight away I was like 'for sure he closed the door way too early.'"
Vergne defended his part in the crash, saying: "I don't know what he thought, maybe he didn't see the corner because he was completely on the inside. He completely misjudged the braking.
"I don't want to talk bad about Nelson, I'm just happy he's OK because it was a big crash. But it was unnecessary."