Obafemi Martins broke his African Nations Cup duck by scoring two quick goals as Nigeria fought back to beat Senegal 2-1 and qualify for the quarter-finals on Tuesday.
Senegal also qualified, rescued by a shock 2-1 defeat for World Cup qualifiers Ghana against rank outsiders Zimbabwe.
Nigeria, who needed only a draw to make sure of their place, topped Group D with nine points from three games while Senegal, Ghana and Zimbabwe all finished with three.
However, Senegal had the best goal difference.
Nigeria will meet Tunisia in Port Said on Saturday while Senegal travel to Alexandria to face Guinea on Friday.
In the other ties, hosts Egypt play Democratic Republic of Congo in Cairo on Friday and Cameroon play Ivory Coast on Saturday.
Inter Milan striker Martins, playing his first Nations Cup, scored twice in nine minutes after failing to hit the target in the previous match.
"I dedicate these goals to my mother and my brothers," said the 21-year-old. "I'm very happy to have scored two goals in such as important match."
His first goal came after the normally reliable Senegal goalkeeper Tony Sylva spilt Nwankwu Kanu's cross and Martins headed home from almost on the goalline.
Nine minutes later, Kanu headed back across goal following a free kick and Martins was on hand to score the winner.
John Obi Mikel, who plays for Lyn Oslo and is the subject of a bitter transfer row between English Premier League clubs Chelsea and Manchester United, started his first competitive match for Nigeria.
He produced some nice touches in midfield without setting the match alight.
Senegal had dominated large parts of the match and deservedly went ahead when Souleymane Camara hammered home a rebound in the 59th minute after goalkeeper Vincent Enyeama had parried Henri Camara's shot.
The game turned in the 77th minute when Enyeana made a superb save to turn away a powerful volley by Henri Camara.
"We could have been 3-0 down at halftime and in the second half as well," said Nigeria coach Augustine Eguavoen.
His Senegalese counterpart Abdoulaye Sarr said: "We dominated 80 percent of the game but in football there's another factor which is the last 10 minutes.
"The first goal was an individual mistake and it demoralised the team. We needed to be strong but we weren't."
In the event, a draw would have been enough for Ghana but instead they were upset by Zimbabwe, who have performed creditably throughout the tournament.
Belgian-based Cephas Chimedza opened the scoring on the hour before Portsmouth striker Benjani Mwaruwari galloped through the defence to add a second ten minutes later.
Baba Ademu's injury-time goal was too late for Ghana.