Port-Gentil - Mahmoud Kahraba scrambled in the winner in the 88th minute as Egypt beat Herve Renard's Morocco 1-0 on Sunday to reach the semi-finals of the Africa Cup of Nations.
A tense last-eight tie in Port-Gentil seemed destined for extra time until Kahraba, on as a substitute, stabbed the ball over the line after the Moroccan defence had failed to cut out a corner.
The Pharaohs, record seven-time winners of the trophy, now advance to a semi-final against Burkina Faso in Libreville on Wednesday after claiming a rare victory against their north African rivals.
The 1-0 scoreline also means Hector Cuper's side have still not conceded a goal in four games in Gabon.
"It was a very complicated match for both teams for 94 minutes. We both had chances but we were lucky to get the goal from a dead ball," said Argentine Cuper.
Defeat for Morocco means Renard, following previous triumphs with Zambia and the Ivory Coast, will not become the first man to win a third Cup of Nations title here with a third different country.
"Congratulations to Egypt and their coach," said Renard.
"They are a very, very strong team who on a difficult pitch managed to play football, so when they find themselves on a better pitch it will be difficult to beat them."
Given the identities of the two coaches, this tie always looked likely to be a close-fought tactical battle and so it proved on a dreadful pitch in Port-Gentil that the tournament can be glad to see the back of.
Renard opted to play with three centre-backs, Romain Saiss joining Manuel Da Costa and skipper Mehdi Benatia in the middle of the defence as they looked to suffocate the Egyptian attack.
It was a largely successful tactic but the first half was almost totally devoid of real incident.
Egypt did threaten once when Nabil Dirar failed to clear at the back post and Trezeguet's effort was saved by Munir Mohamedi.
At the other end, Saiss was unable to connect with Da Costa's ball across the face of goal but then headed against the top of the bar as the Egyptians struggled to clear.
Cuper had lost Arsenal's Mohamed Elneny to injury earlier in the day and he then saw Marwan Mohsen limp off late in the first half, the striker the latest of many victims of the pitch over the last two weeks.
A livelier start to the second period saw the Moroccan 'keeper produce a strong arm to block a Mohamed Salah effort before the pocket-sized midfielder M'bark Boussoufa struck the bar at the other end.
The bulk of the support in the stadium was for Morocco and the bulk of the chances were going their way too in their first knockout round appearance since 2004.
But striker Aziz Bouhaddouz, who scored in the group-stage win against Togo, somehow could not connect with Faycal Fajr's driven ball inside the six-yard box and then headed narrowly wide.
Salah was the danger man for the Egyptians and when Abdallah El Said chipped a free-kick over the wall and into his path, the Roma star's effort was spectacularly saved by Munir.
Extra time loomed but then Kahraba struck to spark wild Egyptian celebrations and break Moroccan hearts.