Wilfried Bony missed from the spot as Algeria edged past the Ivory Coast in a dramatic penalty shootout to reach the Africa Cup of Nations semi-finals.
Sofiane Feghouli had fired Les Fennecs into a 20th-minute lead and then saw team-mate Baghdad Bounedjah squander a second-half penalty before Jonathan Kodjia levelled in a tight encounter in Suez.
But substitute Bony and skipper Serey Die both failed from the spot to send Algeria through 4-3 on penalties to set up a last-four clash with Nigeria in Cairo on Sunday.
Kodjia failed to make the most of two early chances and Algeria took full advantage when Amir Bensebaini found space on the left with 20 minutes gone and crossed for Feghouli to sweep a shot past keeper Sylvain Gbohouo.
They should have increased their lead after Bounedjah had been felled by Gbohouo just 23 seconds after the restart, but the striker crashed the spot-kick against the crossbar, and his side was made to pay with 62 minutes gone when Kodjia fired low past Rais Mbohli to level.
Mamadou Bagayoko cleared a Riyad Mahrez shot off the line as Algeria responded, but the sides could not be separated in the 90 minutes and after Gbohouo had beaten away substitute Islam Slimani’s extra-time header, penalties were required.
William Troost-Ekong’s late strike sent Nigeria into the semi-finals of the Africa Cup of Nations at the expense of South Africa.
The Udinese centre-back struck in the 89th minute to seal a 2-1 win at the Cairo International Stadium.
Samuel Chukwueze opened the scoring for Nigeria in the first half only for Bongani Zungu to level in the second half after VAR intervened.
The Super Eagles will play Ivory Coast or Algeria in the semi-finals in Cairo on Sunday.
Chukwueze opened the scoring after 27 minutes when he linked with Arsenal’s Alex Iwobi to score at the second attempt.
Zungu levelled with 20 minutes remaining when he nodded in from a free-kick and it was given after a VAR check showed the ball came off Nigeria’s Odion Ighalo before it fell to an initially offside Zungu.
But Nigeria won it with a minute left when goalkeeper Ronwen Williams missed his punch from Moses Simon’s corner to allow Troost-Ekong to bundle in the winner.
Youssef En-Nesyri scored the only goal as Morocco beat Ivory Coast 1-0 to qualify for the knockout stages at the Africa Cup of Nations.
En-Nesyri scored after 23 minutes as Herve Renard’s side made it successive 1-0 victories in Group D, following their success against Namibia in the opening match.
Romain Saiss made a superb goal-line clearance to deny Jonathan Kodjia in the opening minute before En-Nesyri brilliantly evaded a series of challenges and scored with a low finish.
Max Gradel and Nicolas Pepe worked the Morocco goalkeeper while Kodjia, who scored his side’s winning goal against South Africa, was off target this time.
En-Nesyri went close to a second goal with a header and Noussair Mazraoui also could have made it 2-0 late on but his volley crashed against the crossbar from close range.
Morocco advance while Ivory Coast’s fate will be decided in their final group game against Namibia.