Lalaina Nomenjanahary gave Madagascar a shock 12th-minute lead in Alexandria after pouncing on ponderous defending, jolting a side ranked 63 places above them in the world.
Charles Andriamatsinoro sealed the victory in front of CAF president and Madagascar native Ahmad Ahmad with a heavily deflected free-kick on 53 minutes for his second goal in three games in Egypt.
The Indian Ocean islanders had to come through a preliminary round just to reach the qualifying competition but will advance as winners of Group B ahead of Nigeria, with Guinea hoping to progress as one of four best third-place finishers following a 2-0 win over Burundi in Cairo.
Defeat for Nigeria likely sets up an eye-catching clash with either holders Cameroon or four-time winners Ghana in the next round, while Madagascar will take on a team that finishes third from either Group A, C or D.
Having already qualified after back-to-back 1-0 victories, Nigeria coach Gernot Rohr made five changes to the side that overcame Guinea in their second game with Samuel Kalu handed his first start of the tournament following last week’s collapse in training due to severe hydration.
Yet after a comfortable start Nigeria gifted their opponents a goal completely against the run of play when a poor pass from John Ogu put Leon Balogun in trouble, with Nomenjanahary pinching the ball on the edge of the area and coolly dancing round Ikechukwu Ezenwa to slot home.
Kalu bent a shot wide of the far post in response for Nigeria, whose penalty appeals were ignored when Ahmed Musa tumbled to the ground after trying to skip past goalkeeper Melvin Adrien on the half-hour.
French coach Nicolas Dupuis, who combines the role with his job at a lower-league club in his homeland, has worked wonders with a Madagascar side that qualified for a first Cup of Nations by finishing above Equatorial Guinea and Sudan.
– Guinea face anxious wait –
They doubled their lead early in the second half here when Andriamatsinoro’s set-piece flicked off substitute Wilfred Ndidi and left Ezenwa stranded as the ball sailed into the net.
Ndidi belatedly registered Nigeria’s first shot on target just before the hour with a curling effort straight at Adrien, although Anicet Andrianantenaina nearly bagged a third as Ezenwa smothered on the line after an inviting Andriamatsinoro cross.
While Madagascar became the first debutants to advance beyond the group stage since Cape Verde in 2013, they will be without midfielder Marco Ilaimaharitra for their next game after he collected a late yellow card, his second in three matches.
In contrast, fellow newcomers Burundi bowed out without a single goal or point after a 2-0 loss to Guinea.
Burundi’s hopes suffered a huge blow when Christophe Nduwarugira was sent off on 12 minutes in the capital after a foul as last man, and Mohamed Yattara stepped up for Guinea with both goals to leave them facing a nervous wait for a spot in the last 16.
For more news your way, download The Citizen’s app for iOS and Android.
Download our app and read this and other great stories on the move. Available for Android and iOS.