Zambia beat Gabon 2-1 in Benguela to take top spot while Cameroon twice came from behind to draw 2-2 with Tunisia in Lubango and finish second.
The results saw Zambia and Cameroon end on four points, along with Gabon who had been top of the standings before the last day of group matches at the tournament in Angola.
Zambia, who will face Nigeria in the last eight in Lubango on Monday, finished top by virtue of the number of goals scored against the other three teams.
They were level on points with Cameroon and Gabon and also tied on goal difference but in a mini-league between the trio had scored four goals to Cameroon´s three and Gabon´s two to advance according to tournament regulations.
Cameroon will return to Benguela to play holders Egypt in their quarter-final tie on Monday. Zambia staved off a late rally from Gabon, who scored seven minutes from time to set up the possibility of a late recovery after being thoroughly outplayed for most of the match.
Fabrice do Marcelino's strike ensured a nervy finish for Zambia but they held out to reach the quarter-finals for the first time in their last six tournament appearances.
Rainford Kalaba got their first, beating the onrushing Gabon goalkeeper to a finely threaded through ball in the 28th minute. The second in the 62nd was a simple tap-in for James Chamanga.
"People were laughing at our chances and all the betting had us as the worst team we proved we are good enough to reach the quarter-finals," said Zambia's youthful French coach Herve Renard.
Cameroon substitute Achille Webo set up two goals as they snatched their ticket to a seventh successive quarter-final appearance.
Webo, who came on at halftime, provided Samuel Eto´o and Landry Nguemo with perfect passes as Cameroon fought back. Tunisia, eliminated after drawing their three games, had gone ahead inside the first minute through Amine Chermiti and were in front again in the 63rd when Cameroon defender Aurelien Chedjou put the ball into his own net.
The Indomitable Lions defence was caught snoozing when striker Chermiti connected with a Khaled Souissi cross to head home after 50 seconds.
But soon after halftime Webo found Eto´o, who coolly slotted home to level the match in the 47th minute.
Centre back Chedjou, however, lobbed his own keeper in an attempt to clear a cross as Tunisia regained the lead. One minute later, Nguemo equalised for Cameroon when he fired past Aymen Mathlouthi from another Webo pass.
Tunisia finished with 10 men after defender Ammar Jemel was sent off for a second bookable offence in the 88th minute.