Morocco claimed top spot in Group D after Mark Boussoufa’s stoppage time winner at the Al Salam Stadium. Hervé Renard’s men will face one of the teams which finished in third place.
Algeria crushed Tanzania 3-0. Algeria boss Djamel Belmadi rested the Manchester City midfielder Riyad Mahrez for the final game in Group C. Despite the skipper's absence, Algeria were too classy for Tanzania.
The goals came in a 10 minute burst just before half-time. Islam Slimani got the opener and that was followed by a brace from Adam Ounas.
Belmadi's charges have looked an ominous outfit in the group stages with their ability to venture seamlessly between silky and steely.
They showed abundance of the latter when edging past Senegal 1-0 on 27 June. And the west Africans bounced back from that defeat with a 3-0 romp past Kenya to secure second place in Group C.
Kenya could have claimed the runners-up spot with a victory but they were unable to replicate the heights of their 3-2 win over Tanzania on 27 June.
Senegal captain Sadio Mané missed a first half penalty but he atoned for his poor marksmanship in the second half with a brace – one of which came from a spot kick.
Ismaila Sarr struck Senegal’s other goal and they will face Uganda on 5 July in the last 16 at the Cairo International Stadium.
Cote d’Ivoire booked their passage as Group D runners-up courtesy of a 4-1 mauling of Namibia.
Max-Alain Gradel opened the scoring six minutes before the break. The veteran striker lifted his finish over the Nambia goalkeeper Lloyd Kazapua.
It was a blow to the Namibians who had been the brighter and more creative of the two sides.
The Ivorians struck again 12 minutes into the second half. Skipper Geoffroy Serey Die surged upfield and with little movement around him from his teammates, decided to let fly from 30 metres. The ball took a deflection and sailed past the right hand of Kazapua.
Joslin Kamatuka halved the deficit with 20 minutes remaining. But Wilfred Zaha extinguished any hopes of a comeback in the 84th minute and Maxwel Cornet added the gloss.
He thumped home after Frank Kessie launched a counterattack after stealing the ball from Petrus Shitembi.
“Once they got into the final third, Cote d’Ivoire were more clinical than us,” said Namibia boss Ricardo Mannetti.
"For a while we made them look average and then they made us look like schoolboys. We need to get our players to play in better leagues because most of our exports are to South Africa and we only have two European based players.”