Starting seventh on the grid, Ocon improved a rung to finish sixth ahead of Kevin Magnussen of Haas, Carlos Sainz of Toro Rosso, who also survived a first-lap collision, two-time champion Fernando Alonso, who scored McLaren's first points of the year in ninth, and Pascal Wehrlein of Sauber.
Perez, who started sixth on the grid, however failed to complete the race as he withdrew in the 39th lap following a collision.
Championship leader Sebastian Vettel beat Lewis Hamilton to fourth place, despite being penalised for dangerous driving after hitting his Mercedes rival behind the safety car.
It was an afternoon of non-stop drama at the Baku City Circuit, featuring no less than three safety-car periods and a red-flag race stoppage.