Perez nabbed pole on his first Q3 run as team-mate Max Verstappen made a mistake and aborted his lap, with Perez leading Aston Martin driver Fernando Alonso and Ferrari's Carlos Sainz.
Leclerc then crashed on the second run, bringing out a red flag that ended the session without anyone getting a second lap on the board, locking Perez in for pole.
The result came as a surprise to the Mexican, who had been struggling for pace compared to his team-mate and the Ferraris until that point.
In Q1 Perez was third 0.350s behind Verstappen, while in Q2 Perez dropped to fifth over half a second in arrears. In FP3 he had also been half a second off the Dutchman.
"I think it's been my worst weekend up to qualifying, really," he said.
"I just couldn't figure out how to pull those tenths that I was missing all the time to Max and to the Ferraris. I was just resetting everything.
"We did a small change into qualifying and everything became more alive. And I think with this tarmac we were just playing a bit with the tools, and we put the lap in when it mattered."
Perez explained he had been struggling for balance on Miami's brand-new tarmac, which offers little grip and caught several drivers out.
When asked what he was missing, he said: "Just everything. It wasn't coming together.
"It's one of those weekends where I was just struggling for balance, confidence. This tarmac is very sensitive to temperature."
With Verstappen starting down in ninth, Sunday's Miami GP offers a great opportunity for Perez to not only take his third win of the season but also overtake his Red Bull partner in the drivers' championship for the first time in his Red Bull career.
"I'm just thinking race by race I will go out, cheer for my team because they've done a tremendous job and yeah see what happens tomorrow," he added.
"Tomorrow it's a new opportunity starting from pole."