Algeria booked an Africa Cup of Nations semi-final with Nigeria after battling past Ivory Coast on penalties.
Similarly, the Eagles of Carthage, who will face Senegal in Sunday’s last-four tie, led through Ferjani Sassi’s deflected shot from the edge of the box.
For Tunisians, Youssef Msakni then doubled their lead after Wahbi Khazri’s shot was parried by Melvin Adrien.
Naim Sliti added an injury-time third to cap off a counter-attack as Madagascar sent players forward.
Indian Ocean island side Madagascar, ranked 108th in the world, were the story of their debut tournament. They had to start Afcon qualifying in the preliminary round in March 2017, but went on to stun Nigeria in the group stages last month and then DR Congo in the last 16.
Their squad contains players who play in Reunion, the French lower leagues and Thailand. Their head coach Nicolas Dupuis also manages French fourth-tier Fleury.
But their dream came to an end as they were well beaten by a professional Tunisia performance in Cairo.
Tunisia playmaker Khazri was heavily involved in proceedings throughout and had a free-kick tipped over the bar by Adrien. He then thought he had scored their opener, but it was disallowed correctly for offside.
Tunisia got the breakthrough when Sassi’s shot hit the backside of Thomas Fontaine and flew into the net. And the game was over once Msakni scored.
The underdogs, who never looked like scoring, took risks in a bid to find a way back into the game – and Khazri squared the ball for Sliti to finish off a quick break and seal the win.
Tunisia reached their first Africa Cup of Nations semi-final since 2004 as they ended Madagascar’s fairytale run.
Sofiane Feghouli broke the deadlock in the first half for Algeria ahead after Baghdad Bounedjah squared it to the Galatasaray midfielder.
After the break, Bounedjah hit the bar from the spot after earning Algeria a penalty, and they were made to pay just after the hour-mark when the Ivory Coast equalised through Aston Villa forward Jonathan Kodjia, who rode a tackle before firing a superb effort into the far corner.
The match ultimately went the distance, and after five perfect penalties, Wilfried Bony saw his penalty saved, and though Youcef Belaili could only hit the post with Algeria’s fifth penalty, Serey Die did likewise to send the Ivory Coast packing.
It was the Ivorians who started the brighter of the two teams, with Max Gradel rattling the post from distance and Kodjia failing to apply the finish from close range after superb work from Wilfried Zaha on the right.
Algeria had already shown a threat, with Riyad Mahrez dragging a decent effort wide before they took the lead. Bagayoko rashly challenged Bounedjah and the loose ball fell to Bensebaini, who squared it to Feghouli to finish wonderfully.
The first five minutes of the second half saw plenty of drama. Firstly, Algeria missed a golden opportunity to double their lead, with Bounedjah missing a penalty he took after being cleaned out by Sylvain Gbohouo.
Seconds later, Kodjia’s attempted pass across the Algerian box took a nick off Aissa Mandi and hit the bar. Eventually, Bounedjah’s penalty miss proved costly, with Kodjia gathering a ball from Zaha brilliantly, skipping past his maker and driving the ball into the bottom corner.
That was enough to force extra time, which failed to separate the two teams. In the shoot-out, Raiss M’Bolhi saved Wilfried Bony’s effort and although Youcef Belaili hit the post, a subsequent miss from Serey Die sent the Algerians through.
They face Senegal at 17:00 BST on Sunday, with Algeria facing Nigeria at 20:00 BST.