Egypt, who endured a torrid start to the competition when they trailed Brazil 3-0 in their opening match, completed a remarkable recovery by reaching the men's football quarter-finals at the Olympics with a 3-1 win over Belarus on Wednesday.
Gold medal favourites Brazil completed their perfect run of three straight wins when they beat New Zealand 3-0 at St James' Park in Newcastle.
Egypt, bedraggled and bewildered against Brazil at the start, finished the match narrow 3-2 losers and inspired by that fightback, finished the group phase in some style.
They created plenty of chances and picked up their first point with a 1-1 draw against New Zealand on Saturday and leapfrogged Belarus into second place in Group C in their last match by beating them at Hampden Park, Glasgow.
Belarus just needed a draw to qualify but Egypt, who finished second to Brazil with four points, will now face either Japan, Honduras or Morocco in the quarter-finals at Old Trafford on Saturday.
They won with goals from Mohamed Salah, substitute Marwan Mohsen and Mohamed Aboutrika, whose third in the 78th minute sealed the game. Andrei Voronkov scored a late consolation for Belarus four minutes from time.
Brazil had their match against New Zealand wrapped up in the first 30 minutes. Danilo and Leandro Damiao scored the first two and Sandro added the third six minutes into the second half.
Those two matches opened the final round of group matches with another six matches scheduled for later including hosts Britain who take on Uruguay in Cardiff, with the Olympic hosts needing just a draw to make sure of a place in the last eight.