(CNN) -- Zambia and Cameroon have reached the African Nations Cup quarterfinals as a dramatic final day in Group D saw Gabon -- who were top before kick-off -- miss out on qualification.
The top three teams all finished on four points, but Gabon missed out by virtue of scoring fewer goals, while Tunisia ended up bottom of the table on three points after drawing all their three matches.
Goals from Rainford Kalaba and James Chamanga earned Herve Renard's table-topping Zambia side a 2-1 victory against Gabon in Benguela despite substitute Fabrice Do Marcolino's deflected late consolation strike.
But the win came at a price, with both Kabala and center-half Kampamba Chintu collecting bookings which rule them out of their last eight clash against Group C runners-up Nigeria.
Meanwhile, Cameroon booked their place in the last eight after twice coming from behind to seal a 2-2 draw against Tunisia in Lubango.
The Indomitable Lions, who needed at least a point to ensure qualification, fell behind after just 57 seconds thanks to a fine diving header from Amine Chermiti.
It stayed at 1-0 until a minute after half-time, when Samuel Eto'o equalized with a close-range strike, before an own goal from Aurelien Chedjou restored the advantage for the Carthage Eagles.
But Landry N'Guemo responded instantly with an arrowed finish to hand Paul Le Guen's team a place in the next stage -- where they will face two-time defending champions Egypt for a place in the last four of the competition.