Rainford Kalaba and James Chamanga scored for Zambia, which reached the quarterfinals for the first time in 14 years.
"People were laughing at our chances and all the betting had us as the worst team here," said Zambia's French coach, Herve Renard, "but today we proved we are good enough to reach the quarterfinals."
Cameroon's 2-2 tie with Tunisia meant that Zambia, Cameroon and Gabon finished tied with 1-1-1 records and were even on goal difference.
Zambia took first place with four goals, Cameroon was second with three and Gabon with third with two in head-to-head play.
For Gabon, which opened with a win over Cameroon and tie with Tunisia, the elimination was a shock.
"We thought we had qualified but then they told us no," said Gabonese captain Daniel Cousin. "It is heartbreaking, but this is what sport is all about."