Youssef Hadji and Jawad Zairi scored extra-time goals as Morocco came from behind to beat Algeria 3-1 in an African Nations Cup quarter-final thriller here on Sunday.
Abdelmalek Cherrad thought he had put the underdogs into a semi-final showdown against Mali with an 84th-minute goal only for Marouane Chamakh to equalise four minutes into stoppage time.
Hadji then scored with an acute-angle shot under goalkeeper Lounes Gaouaoui in the 113th minute after leaving defender Samir Beloufa standing and Zairi sprung an offside trap to deliver the killer blow in the final minute.
Morocco were the third team after Mali and Nigeria to reach the semi-finals despite falling behind.
Hosts Tunisia face Nigeria in Tunis Wednesday three hours before Morocco tackle Mali in Sousse.
The Atlas Lions made one change from the team that held South Africa to top Group D with Chamakh coming in on the right side of midfield for another France-based professional, Hadji.
Algeria, surprise qualifiers at the expense of four-time champions Egypt in Group C, retained only four of the team that lost to Zimbabwe and finished runners-up to defending champions Cameroon.
There was a strong security presence at the 22,000-seat Tayeb Mhiri Stadium in the eastern port city to supervise a capacity crowd with the green of Algeria the dominant colour.
But there was little to excite the supporters during a first half dominated by defences in the first Nations Cup clash of the North African neighbours since they drew 1-1 in Morocco 16 years ago.
The busiest person on the pitch was often Libyan referee Abdulhakim Shelmani, who cautioned two players from each side, but surprisingly failed to raise a card when pushed by Mamar Mamouni after Morocco were awarded a free kick.
Morocco did have the ball in the net after five minutes, but the whistle blew for a foul before Jawad Zairi, a young striker being compared to former Argentina star Diego Maradona, netted with a close-range shot.
Zairi, one of many France-based professionals appearing at the biennial showcase of African football, was the centre point of the Moroccan attack only to be continually caught in an offside trap.
Algeria captain Djamel Belmadi would have hoped for more when he cut in from the right and laid the ball back into the path of fellow midfielder Yazid Mansouri, whose woeful finishing saw the ball fly into the crowd.
The half ended without goalkeepers Lounes Gaouaoui of Algeria or Khalid Fouhami being tested and only two corners conceded, both by Algeria late in a half best forgotten.
Morocco almost snatched the lead as Chamakh sprinted after a weakly hit back pass and Gaouaoui had to race off his line to boot the ball clear.
Whatever coaches Rabah Saadane and opposite number Badou Zaki said during the break had a positive effect with both teams more attack minded and Youssef Mokhtari stung the fingers of the Algerian goalkeeper, who parried a shot that was cleared.
As the match reached the hour mark, 1976 champions Morocco wasted a glorious chance to end the deadlock when defender Talal al-Karkouri blazed over.
Morocco were pressing more and came close again 11 minutes from full-time as Gaouaoui raced outside his penalty area only for Houssine Kharja to head the ball over him, but Yahia calmly cleared at the expense of a corner.
When the stalemate ended it was Algeria who scored six minutes from full-time as Cherrad beat Fouhami with a flying header after a Hocine Achiou cross was deflected into his path.
However, four minutes into stoppage time Morocco finally struck when a cross from substitute Mohamed Yaacoubi eluded three defenders and unmarked Chamakh beat onrushing Gaouaoui with a rising, right-foot shot to force extra time.