Robert Lewandowski's neat chip put Dortmund ahead in the fourth minute, but the lead lasted only 10 minutes before Souleymane Diawara headed home an equaliser.
With the score at 1-1 and with Napoli winning against Arsenal, the 2013 finalists looked to be heading out of Europe's premier club competition, despite playing against 10 men following the first-half dismissal of OM midfielder Dimitri Payet.
But, in the 86th minute, Kevin Grosskreutz popped up to fire the German side into the knockout stages.
The result meant Napoli dropped into the Europa League despite their 2-0 triumph over the Gunners in Naples boosting them onto 12 points, with Dortmund advancing as group winners.
Lewandowski gave the visitors the perfect start at the Stade Velodrome, sprinting onto Eric Durm's fine ball over the top and delicately lifting the ball over the advancing Steve Mandanda with less than four minutes on the clock.
Nuri Sahin had half a chance to double the German side's lead but lashed a 25-yard attempt over the bar in the 10th minute, and soon after the visitors were pegged back.
Saber Khelifa beat Roman Weidenfeller in the air and powered a header from Payet's free-kick against the bar, and Diawara was on hand to nod the rebound into the empty net.
OM's cause suffered a severe setback just after the half-hour mark, though, when Payet was sent off for two bookable offences in the space of three minutes.
The France midfielder was shown a first yellow card for a foul on Jakub Blaszczykowski and then, moments later, went down in the Dortmund penalty area and received a second caution for simulation.
Dortmund sought to press home their numerical advantage before the break, but were restricted to long-range efforts from Grosskreutz and Marco Reus which were both blocked.
As things stood at half-time, Dortmund were going through, but needed a win to guarantee their place in the knockout phase.
And they started the second half seemingly determined to get all three points.
Blaszczykowski saw his header parried by Mandanda and Reus' fierce effort cannoned against the bar from 20 yards, before Blaszczykowski tried his luck again with a 20-yard attempt which was blocked by Mario Lemina.
Lewandowski and Reus mustered further efforts which failed to trouble Mandanda and then there was suddenly more urgency needed when news filtered through from Naples that the hosts had forged ahead against Arsenal.
Jurgen Klopp's team looked to be heading out of the Champions League but, with four minutes to go, Grosskreutz latched onto Henrikh Mkhitaryan's cross and diverted the ball past Mandanda at his near post despite losing his footing.