Morocco, with an injury time penalty, believed they had done enough to keep their Cup hopes alive, but that wasn't counting on Bruno Mbanangoyes's curling free-kick in the final second of the eighth minute of stoppage time of a remarkable encounter.
Gabon had been on the brink of winning in normal time after second-half goals from Daniel Cousin and Pierre-Emerick Aubameyang had put them in command after Houssine Kharja's opener. But, as the stadium announcer urged the 40,000 partisan supporters to "remain calm, remain calm" Kharja appeared to have spoiled Gabon's night when stepping up to coolly convert the 91st minute spot-kick awarded for Charly Moussono's handball.
Reflecting on the stunning win, Gabon coach Gernot Rohr said: "The final 15 minutes were truly dramatic. I must say bravo to my players. Our fortunes turned when Pierre-Emerick Aubameyang scored. Now we'd like to finish top of the group, then we can stay in Libreville."
Morocco coach Eric Gerets was bemused how the game had slipped from his team's grasp, and marvelled at the dramatic finish. "My heart was going 200 beats a second! I have to congratulate Gabon's coach and his team, they'll have a big party in Gabon tonight, but I would have preferred a big party in Morocco."
Gerets made four changes from the opening defeat to Tunisia. The Belgian chose to start Arsenal striker Marouane Chamakh, laid low with a stomach bug this week, on the bench, where he was joined by Noureddine Amrabat, Mbark Bousoufa and Ossama Assaidi, recovering from an Achilles tendon injury.
His Gabon counterpart Gernot Rohr named the same side that saw off Niger 2-0 on Monday. As they had done in their opening match, Rohr's co-hosts chased and harried their north African visitors, whose Michael Basser had Gabon's defence on the rack early on only to blast his shot off target.
On 24 minutes, Morocco injected some much needed life into their campaign when Younes Belhanda found Kharja in the middle of the area, the Fiorentina midfielder losing his marker and shooting low and left-footed past Gabon keeper Didier Ovono Ebang.
At the restart Gabon introduced Cousin for Stephane Nguema and Morocco replaced Youssef El Arabi with Noureddine Amrabat. Cousin's introduction almost paid immediate dividends when the former Hull City striker raced onto a cute backpass from Aubameyang over in the right-hand corner, his venomous shot smacking off keeper Nadir Lamyaghri's outstretched hands.
On the hour mark, Remy Ebanega was stretchered off with blood pouring from his mouth, the defender returning to the field of battle with a bandaged head. Queen Parks Rangers striker Adel Taarabt then entered the fray as Morocco tried to kill the game off.
But Gabon got the equaliser they deserved in the 77th minute when Aubameyang volleyed in after a move begun by a long throw from the left touchline. Seconds later Cousin sent the 40,000 crowd into ecstasy when bagging what he and the rest of his teammates thought was the winner, skilfully turning to lose his marker, the ball going in off the right post.
But the game had only just begun, as Kharja converted from the spot and then Dinamo Minsk midfielder Mbanangoye became a national hero.