Tunisia reached their first Africa Cup of Nations semi-final since 2004 as they ended Madagascar's fairytale run.
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.
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.