Cameroon finished top of Group C after a dreary, niggly goalless draw with Egypt while Algeria clung on to second place despite a shock 2-1 defeat by debutants Zimbabwe, who won a game at the finals for the first time.
Algeria and Egypt finished level on points and goal difference but the Algerians qualified on goals scored. Zimbabwe were last with three points.
Algeria were on their way out when they trailed 2-0 until midfielder Hocine Achiou scored a precious 73rd-minute goal, pouncing on a blunder by Zimbabwe's teenage goalkeeper Tapuwa Kapini to head home.
"It was a miracle we made it back," Algeria coach Radah Saadane said.
Egypt had been among the pre-tournament favourites and their failure to progress prompted coach Mohsen Salah, who was previously in charge in 1995 until he fell out with several key players, to end his second stint in the job.
"I have worked very hard but I have failed," he told reporters. "When I get back to Egypt, I will resign and another coach will take over. Good luck to him."
Cameroon and Algeria joined Tunisia, Guinea, Mali and Senegal in the last eight. The line-up will be complete on Wednesday after Nigeria meet Benin and South Africa play Morocco in Group D.
CONTROVERSIAL KIT
Cameroon, winners of the last two Nations Cups, took the field in Monastir wearing their controversial one-piece kit even though world body FIFA announced last week that it is banned because teams must wear separate shirts and shorts.
The two sides, incapable of stringing more than a couple of passes together, produced one of the tournament's poorest matches with persistent niggling and play-acting interrupting play.
Egypt were closest to victory when midfielder Tarek El Sayed hit the post with a rasping left-foot drive in the 66th minute.
"Today was not a good match. It was 60, 70 percent of what Cameroon can do in football terms. We lost too many balls in midfield," said Cameroon's German coach Winfried Schaefer.
In nearby Sousse, Algeria were again backed by thousands of countrymen who flocked across the border but Zimbabwe, who lost their two opening games, dominated the match.
Adam Ndlovu, who had missed two easy first-half chances, gave Zimbabwe the lead in the 65th minute when he headed home a corner from his younger brother Peter.
Six minutes later Joel Luphahla, who plays club football in Cyprus, strode through on the break for a second goal as Algeria suddenly found themselves in dire straits.
Achiou, scorer of the late winner in the 2-1 win against Egypt last week, came to the rescue and as news filtered through that Egypt had drawn 0-0, the Algerians began to celebrate.
"This was a great result for us. We let ourselves down (in the first game) against Egypt, otherwise we might have even made the quarter-finals," said Peter Ndlovu, who plays for Sheffield United in the English first division.