Benin produced a major surprise to beat Morocco 4-1 on penalties and book a quarter-final spot at the Africa Cup of Nations for the first time.
Mama Seibou hit the winning penalty to send 10-man Benin through after Khaled Adenon had been sent off in extra time.
In an incident-packed tie in Cairo, Moise Adilehou put Benin ahead before Youssef En-Nesyri equalised.
Morocco wasted a chance to win it with the last kick of normal time but Ajax's Hakim Ziyech's penalty struck a post.
Benin scored all their penalties in the shootout through Olivier Verdon, David Djigla and Tidjani Anaane before Seibou's effort sparked wild celebrations on and off the pitch inside the Al Salam Stadium.
In what is only their fourth appearance at the Africa Cup of Nations, Benin will face the winners of Friday's late last-16 tie between Uganda and Senegal in the quarter-finals on 10 July.
Wasteful Morocco pay the price
Morocco had progressed from the group stage with three wins and without conceding a goal.
Despite peppering Benin's goal with 22 shots during the 120 minutes, they lacked cutting edge and are now on their way home from a tournament they were expected to do well in.
En-Nesyri and Ziyech both forced saves from keeper Saturnin Allagbe before Benin took the lead through Adilehou's close-range volley following a corner.
Morocco were gifted their equaliser, an horrendous mistake by Jordan Adeoti allowing Mbark Boussoufa to set up En-Nesyri to fire under Allagbe.
In the dying moments of normal time, Morocco were awarded a penalty when former West Brom and Sunderland midfielder Stephane Sessegnon fouled Achraf Hakimi.
However, Ziyech's attempt hit a post and rebounded to safety.
Benin, who advanced to the last-16 as one of the best four third-placed teams in the group stage, played the final 23 minutes of extra time with 10 men when Adenon was sent off for a second bookable offence.
Morocco struggled to make the extra-man advantage count and paid the price as they lost the shootout.
Southampton's Sofiane Boufal fired over the bar after Oussama Idrissi had scored Morocco's opening penalty before En-Nesyri's attempt was brilliantly diverted on to the bar by Allagbe.