Argentinian referee Facundo Tello presents a yellow card to an Algeria player during the FIFA Arab Cup 2021 group D football match between Algeria and Egypt at the Al-Janoub Stadium in the Qatari city of Al-Wakrah on December 7, 2021 (AFP)
Both sides were equal on seven points and they were also tied on a plus-six goal difference and on goals scored, leaving fair play to determine the group leaders.
In a bizarre yet exciting fair-play duel, Egypt and Algeria were neck and neck in yellow cards but striker Hilal Soudani was booked in stoppage time and youngster Yacine Titraoui was then dismissed following a VAR review after a studs-up challenge on Mohanad Lashin.
Egypt will now take on Jordan in the quarters, with Algeria facing a stern test against North African rivals Morocco.
A fluke goal from Mohamed Amine Tougai put Algeria ahead in the first half but Egypt came on with all guns blazing in the second half, even though they lost defender Ahmed Hegazi and midfielder Ayman Ashraf to injury before the break.
A period of sustained pressure in a high-octane half saw Egypt awarded a penalty after Tougai kicked the foot of Ahly midfielder Amr El-Sulaya as he attempted to clear the ball. The referee pointed to the spot after checking a video replay, with El-Sulaya tucking it away, although keeper Rais M'Bolhi got a hand to it.
Chances were few and far between in a largely physical and fast-paced first half, with Egypt pressing Algeria high up the pitch but appearing vulnerable at the back at times.
At the other end, Algeria's impressive midfielder Yacine Brahimi dictated play with his deft touches and neat dribbles as Egypt failed to contain him.
Egypt also looked toothless upfront, with Mohamed Sherif once again producing a disappointing display as a winger and lone striker Marwan Hamdi unable to cause much trouble in the final third.
Algeria broke the deadlock after 19 minutes via a fortuitous goal. After a touched-on free-kick on the edge of the area, Youcef Belaili's attempted shot went across the box and found Tougai at the far post to drill a low effort into the bottom corner after it had deflected off Ahmed Fattouh and wrong-footed keeper Mohamed El-Shennawi.
Egypt were more aggressive in the second half, pouring forward en masse as Algeria looked inferior in terms of fitness levels.
Belaili was unlucky not to double Algeria's advantage early on though, having skipped past a couple of challengers to jink into the area from the left and hit the side netting with a low shot.
Egypt then assumed control and were rewarded on the hour mark when El-Sulaya converted a penalty given for a foul committed by Tougai, who was left in no-man's-land after M'Bolhi had fumbled a corner.
It was then a matter of who would avoid getting unnecessary cards, with Egypt emerging winners.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)