Former Stoke City winger Ramadan Sobhi equalised and then created the winner as hosts Egypt edged out Ghana 3-2 at the Under-23 Africa Cup of Nations.
Earlier on Monday Mali's chances of progressing at the tournament that also decides Africa's three places at the 2020 Tokyo Olympic Games ended as they lost 1-0 to Cameroon.
The results mean Egypt are top of Group A with six points after two matches with Cameroon second on four points, Ghana have one and Mali are pointless.
Egypt will finish top provided they avoid losing to Cameroon on Thursday while Ghana must defeat Mali and hope Cameroon lose to have a chance of reaching the semi-finals.
Ghana led Egypt twice in a seesaw thriller at the Cairo International Stadium before ex-Stoke City and Huddersfield Town attacker Sobhi levelled on 82 minutes.
With two minutes of regular time remaining, Sobhi, who now plays for Egyptian giants Al Ahly, swung a corner in for Ahmed Rayan to head home powerfully for a dramatic victory.
Captain Yaw Yeboah had given Ghana a sixth-minute lead that Egypt's Mostafa Mohamed cancelled out on 17 minutes with his second goal of the tournament.
A weak back pass from Mohamed Abdelsalam allowed Samuel Obeng to put Ghana ahead again just one minute into the second half as he went past goalkeeper Mohamed Sobhy and tapped in.
Egypt then attacked relentlessly and had what they believed was a 57th-minute equaliser disallowed with replays showing Sobhy was marginally offside.
The hosts were without Nasser Maher after he was ruled out for six weeks with a shoulder injury that he picked up in Friday's 1-0 win over Mali.
Cameroon's win over Mali came thanks to a goal from Franck Evina, who is on the books of European giants Bayern Munich, in the 76th-minute.
Mali failed to clear a Olivier Mbaizo cross and Evina netted for the second successive match with a clinical finish that gave goalkeeper Koita Youssouf no chance.
The countries finishing first, second and third will represent Africa in Tokyo and try to emulate gold medal winners Nigeria (1996) and Cameroon (2000).
Olympic qualifiers are restricted to under-23 footballers, but three 'over-age' players are permitted at the finals.