Portugal were sent crashing out of the Olympic football tournament as Costa Rica won 4-2 to reach the last eight.
Brenes sealed Costa Rica's passage
Newcastle's Hugo Viana and Joao Paulo were both sent off as Portugal, already without the banned Cristiano Ronaldo and Luis Boa Morte, were humbled.
Pablo Brenes was Costa Rica's hero as his injury-time goal sent his team through on number of goals scored.
Italy also suffered a shock 1-0 defeat to Paraguay but still reached the last eight after Japan's 1-0 win over Ghana.
Costa Rica's reward is a quarter-final gainst favourites Argentina on Saturday.
Italy, coached by former World Cup winner Claudio Gentile, went through as runners-up and will face Mali in the last eight while Paraguay took top spot and will meet South Korea.
Iraq's fairytale return to the tournament suffered its first setback when they were beaten 2-1 by Morocco.
But their two earlier group wins had already guaranteed them a quarter-final, where they will face Australia for a spot in the semi-finals.