Gabon snatched a late winner to progress to the Africa Cup of Nations last eight as they beat Morocco 3-2.
Morocco had led 1-0 for much of the game, through captain Houssine Kharjah's first-half opener.
But goals from Pierre-Emerick Aubameyang and Daniel Cousin put the co-hosts ahead, before Kharjah's penalty looked to have levelled it up.
Bruno Zita Mbanangoye then curled in a free-kick deep into injury time to seal a dramatic win and send Morocco out.
The frenetic atmosphere in Libreville added to a match that swung back and forth, but ultimately saw the Gabonese come out on top in stunning fashion.
The Moroccans had initially threatened to spoil the party mood, and were much improved from their 2-1 defeat by Tunisia, with captain Houssine Kharjah opening the scoring on 24 minutes.
Kharjah collected a pinpoint pass from Belhanda, before swapping to his left leg and burying it coolly to Gabon goalkeeper Didier Ovono's left.
Morocco protected their lead by getting in some meaty challenges on the Gabonese, who improved as the game went on, but struggled to take advantage of their opponents conceding a string of free-kicks.
Atlas Lions coach Eric Gerets had made four changes to his line-up and they appeared to be telling, but it was the Gabonese changes in the second half that would alter the course of the game dramatically.
Former Hull City and Rangers defender Cousin came on and immediately began hooking up well with the dynamic and pacey Aubameyang.
The co-hosts piled on the pressure and it paid off on 77 minutes.
Cousin nodded on strongly from a throw-in and Aubameyang steadied himself, before lashing in a wonderful right-foot volley to bring the fans down onto the running track in celebration.
And the Libreville crowd were in raptures again almost immediately, as Cousin made it two goals in as many minutes.
Aubameyang this time turned provider, picking out Cousin in the middle of the area, and the substitute swivelled, before scuffing a shot which trickled into the corner past a stranded Nadir Lamyaghri in the Moroccan goal.
Gabon thought they had won it with that second, but there was more drama to come as Morocco won a penalty for a handball by Moussono in the box, which Kharjah coolly dispatched low to Ovono's left, trebling his tally for the tournament.
But there was one more twist in the tail of an incredible climax, as Mehdi Benatia was very lucky not to see red with a reckless challenge on Andre Biyogo Poko at the edge of the area, conceding a free-kick in a dangerous area.
Up stepped another substitute Bruno Zita Mbanangoye, who whipped in a stunning match-winning free-kick from the edge of the box in the 94th minute, to put Gabon through, the crowd into dreamland and send Morocco packing.
The Panthers' coach Gernot Rohr praised his players for not giving up until the final whistle, while paying tribute the Libreville crowd for their part in the win.
"The turning point came when Pierre-Emerick scored, the crowd gave us new energy to push on," said Rohr.
"But then to lose the 2-1 lead and still fight back and win the game shows an incredible determination from my players."
Morocco coach Eric Gerets said his team lost their tactical discipline in the match's climax, costing them the result.
"Tactically we lost in the last 20 minutes," he said.
"The team did not seem capable of doing what we had done so many times in the past," he said.
Gabon join co-hosts Equatorial Guinea in the quarter-finals, after they had knocked Senegal out, also after just two matches, with another dramatic late winner on Wednesday.