Hakim Ziyech's missed stoppage-time penalty proved extremely costly as 10-man Benin stunned 2019 Africa Cup of Nations title contenders Morocco in a penalty shoot-out to reach the quarter-finals after a 1-1 draw in Cairo.
Moise Adilehou forced home at a corner early in the second half to put Benin on course for a shock win but Youssef En-Nesyri equalised for Morocco before Ajax star Ziyech blew the chance to snatch victory by striking the post from the spot in the 96th minute.
Benin were forced to play most of extra time a man down after Khaled Adenon received a second yellow card for dissent, but they prevailed 4-1 in the shoot-out following misses by Sofiane Boufal and En-Nesyri to set up a clash with Senegal, who beat Uganda 1-0 in their last-16 tie, for a spot in the semi-finals.
"It's not the right time to talk about anything," said Morocco manager Herve Renard, after possibly his final game in charge. "The only thing I would like to say is thank you very much to all the fans who came to Egypt to support us.
"I'm a bit ashamed in front of them, and especially about all the people who were supporting us since more than three years.
"You know in life of course, some people are against you, but this is life. You just have to look forward and only remember the positive things."
Morocco suffered a troubled build-up to the tie at Al Salam Stadium with captain and key defender Medhi Benatia lost to injury while Benin were without forward Steve Mounie due to suspension.
The Atlas Lions had been clinical, determined and efficient so far in Egypt, mirroring the beliefs of Renard, the only coach to win the Cup of Nations with two countries.
Benin, winless in 12 previous matches in the competition, only scraped through to the knockout rounds on the back of three draws after holding both Ghana and reigning champions Cameroon.
En-Nesyri and Ziyech both produced saves in a dreary first half from Saturnin Allagbe, who kept his place in the Benin goal after replacing Fabien Farnolle for the final group game.
Younes Belhanda skewed wide for Morocco shortly after the restart but it was Benin who grabbed a surprise lead when Adilehou broke clear of his marker to turn in a corner on 54 minutes, the Squirrels' first goal since their opening game in Egypt.
Substitute Boufal nearly levelled with his first touch after coming off the bench on the hour, meeting Nordin Amrabat's cross with a downward header that skipped off the grass and flashed just over the top.
A mistake from Jordan Adeoti presented Morocco with the equaliser on 75 minutes, as he was caught in possession on the edge of his area by former Al Jazira playmaker Mbark Boussoufa who squared for En-Nesyri to stroke beyond Allagbe.
Ziyech stung the palms of Allagbe with a free kick while Boussoufa, who scored a last-gasp winner in the 1-0 victory over South Africa, also saw his set piece superbly clawed away by the Benin goalkeeper.
After a quiet start to the tournament Ziyech looked destined to finally deliver when Benin captain Stephane Sessegnon upended Achraf Hakimi deep into stoppage time, but the winger smacked his spot kick against the base of the post.
Adenon's bizarre dismissal, ostensibly for brushing the referee away as he attempted to tend to a cramping teammate, gave Morocco the numerical advantage but Ziyech again squandered an opportunity to send his side through as he blazed over a cutback from Boussoufa.
Morocco were left to count the cost of those misses as Boufal blazed over Morocco's second attempt in the shoot-out before Allagbe tipped En-Nesyri's effort onto the bar, leaving Mama Seibou to fire Benin into the last eight.