BBC Sport, Port Said
Obafemi Martins celebrates after scoring his first goal against Senegal
Nigeria's Obafemi Martins scored two goals in the 79th and 88th minutes to secure a 2-1 victory over Senegal and the top spot in group D.
Senegal missed numerous chances and should have won, but still went through on goal difference despite only picking up three points in three games.
Souleymane Camara had given them the lead in the second half but Henri Camara should have wrapped the match up long before Nigeria's late onslaught.
Henri Camara had the first serious shot on target, a fierce drive Nigeria goalkeeper Vincent Enyeama turned for a corner.
Minutes later, Diomansy Kamara curled a shot over the bar from a good position, after Pape Bouba Diop had released him with a delightful crossfield pass.
But it was Senegal central defender Lamine Diatta who should have scored when he met El Hadji Diouf's precise free-kick, but instead he headed wide.
Nigeria were under pressure, but Martins reminded everyone of his menace when he ran on to an underhit back pass.
The Senegal goalkeeper Tony Sylva rushed off his line to hack clear.
As the half ended, Senegal missed a succession of chances in quick succession.
Diouf headed against the post, Souleymane Camara hit the rebound firmly but it was cleared off the line, and Bouba Diop hoisted his effort over the bar.
Shortly after half time, Eguavoen brought on Nwankwo Kanu - which later proved crucial - and Garba Lawal, as Nigeria attempted to get back in the game.
Within five minutes, though, Senegal finally made their domination pay.
Diouf played in Henri Camara, and although his shot was saved the substitute Souleymane Camara was on hand to thump home the loose ball.
The Senegalese supporters, including the legendary former Senegal striker Jules Bocande, leapt for joy.
Bouba Diop had been a commanding presence in midfield, and he nearly doubled Senegal's lead, thumping a low shot that Enyeama had to scramble to save.
Seconds later, the Nigeria keeper made an even better stop from Henri Camara, as the striker sprinted clear.
With slightly over ten minutes left, Nigeria took advantage of Senegal's poor finishing to restore parity.
Sylva could not hold Kanu's cross from the right, with an appreciative Martins nodding it into empty net.
The skills of the wily Kanu, who came on as a second half substitute, proved vital for the Super Eagles.
He set up Martins' winner, allowing the Inter Milan striker to tap home.
Nigeria face defending champions Tunisia in their quarter-final match on Saturday.
Nigeria coach Austin Eguavoen:
"It was a dramatic group. We were lucky. We could have been two down, but we came back.
"Any team could have advanced from this group, but first place is very important for us.
Senegal coach Abdoulaye Sarr:
"I thank God for qualifying.
"We came to qualify and we did. We played with quality for 80 minutes then fell apart, but I'm still happy.