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 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.
BBC Sport’s Nick Cavell
In truth this really wasn’t a very good game at all. Benin sat deep, they wanted to soak up the pressure that Morocco put on them. They did that. They were looking to maybe break on the counter attack or take their moment at a set-piece, which is what they did. They got a goal from a corner, as they hoped.
Morocco just didn’t pass the ball well enough. There didn’t seem to be enough urgency, until after that Benin goal. Then they stepped up a gear and seemed to really push forward and got the equaliser.
They even got a penalty that could have won them the game and even in extra time, when they had an extra man, they didn’t seem to have any way of playing together as a team.
They have got lots and lots of talent. There are lots of players who are great but they just couldn’t get it together. Benin go through thanks to their determination and defensive ability.