The DS Techeetah driver enjoyed a close-run final with Mortara, taking an advantage that hit a maximum of just 0.138s, before Mortara found half a tenth in the final sector.
Da Costa hence clinched pole position - following up his Jakarta front-row start - with a 1m17.070s, 0.081s clear of Mortara's 1m17.151s.
To make it to the final, da Costa had to beat team-mate Jean-Eric Vergne to the punch in an eventful semi-final clash.
While da Costa took a minor advantage through the opening sector, Vergne threw everything at Turns 7 and 8, taking control of the session in one fell swoop.
But Vergne lost it again two corners later with a small error at Turn 10 and, as the French driver's momentum crumbled, da Costa was in the box seat to progress.
An incredibly close encounter between Mortara and Pascal Wehrlein in their semi-final went the way of the Venturi driver, who fell behind by 0.023s at the end of the first sector before reversing the arrears to 0.019s in his favour.
Mortara then had the whip hand over the rest of the lap, building his momentum and clearing Wehrlein by just under 0.2s at the close of the lap. Vergne starts third, having set a quicker semi-final time over Wehrlein.
Earlier, Mortara had dispatched Mitch Evans in the quarter-final duels in clinical fashion, proving more aggressive over the lap to beat the Jaguar driver by over 0.4s.
Nick Cassidy looked good in the opening part of his duel with Wehrlein, taking the advantage by the close of sector 1 by half a tenth, but the New Zealander was millimetres away from clouting the Turn 7 exit wall.
That put a pin in Cassidy's momentum, giving Wehrlein enough rein to pull ahead and subsequently extend his advantage to make the semis.
Vergne powered past Jake Dennis to make the semis, finding over three-tenths advantage in the opening sector before gently eking out that advantage to progress with a rapid 1m16.982s.
Da Costa then dumped out Oliver Askew in the second Andretti, as the American squad got both cars into the final eight for the first time this season.
Askew was just a tenth and a half behind by the close of the first sector, but da Costa turned up the wick and cruised past the former IndyCar driver.
Following the ordering of times, Dennis starts fifth despite having had minimal running so far through the weekend after missing FP1 with a powertrain issue. Evans takes sixth on the grid alongside him, while Askew starts from seventh ahead of Cassidy.
Championship leader Stoffel Vandoorne was not only a shock departure through the opening group of qualifying, but the Mercedes driver was slowest in the session - his car struggling with locking under braking throughout his final lap.
The Belgian was handed a marginal reprieve, however, when Andre Lotterer had all of his lap times having failed to set the mandatory time within the first six minutes of the session.
Lotterer began the session with a sensor issue and thus was late in leaving the pitlane. He missed the line by 0.04s on his first timed lap.
Mercedes' difficult day continued in Group B as Nyck de Vries was shuffled out of the top four by Wehrlein, who snatched third late into the second 12-minute session to claim a place in the quarter-finals.
The damage to de Vries' day was less pronounced, however, as the Dutchman starts ninth from di Grassi - who was also a group-stage casualty after Cassidy dumped him out of the top four late in the session.
Antonio Giovinazzi sustained a puncture at the start of the session and was unable to set a lap within the first six minutes, resigning him to a back-row start with Lotterer.
Marrakech E-Prix: Full qualifying results