Egypt looked set to finish third behind the Blue Sharks and Ghana before the break with timely goals in both games.
Egypt made it to the last 16 at the Africa Cup of Nations after drawing 2-2 with Cape Verde after a dramatic Group B finish in Abidjan.
Pharaohs forward Mostafa Mohamed scored a goal in the 93rd minute, becoming the winner.
However, Cape Verde equalized six minutes later and the Pharaohs secured second place as Ghana went 2-0 up in added time against Mozambique.
Rui Vitoria’s side will face the second-placed team in Group F in the last 16 years on Sunday (20:00GMT).
World Football on the Afcon Podcast: Egypt break through despite injured Salah to return to Liverpool’s coffers Egypt, with an injured Mohamed Salah, trailed at the break through a smart finish by Crumpsimol but half-time substitute Trezeguet equalized in the 50th minute.
The Pharaohs knew a win would be good enough to put them in second place and, with Ghana leading Mozambique, they celebrated as Mohamed lobbed Vozinha to make it 2-1 in added time.
However, drama ensued after the video assistant referee (VAR) sent match official Alhadi Mahamat to his pitchside monitor to judge whether Mohamed had controlled Trezeguet’s ball forward with his chest or arm.
The Chadian official eventually confirmed the goal, however Egypt almost dismissed all their hard work when Brian Texeria fired home from a tight angle.
However, Ghana somehow considered conceding a 91st-minute penalty and a 94th-minute equalizer against Mozambique, which meant Egypt progressed to the knockout stages despite drawing all three of their group games in Ivory Coast.