The defeat left Brazil struggling to digest their worst performance in the Copa America since 1987, the last occasion the five-time world champions failed to make it past the group stage.
Brazil, needing only a draw to qualify for the quarter-finals as winners of Group B, looked to be on course for a place in the last eight with 15 minutes to go against Peru.
But a rare foray into Brazilian territory from Peru ended with Raul Ruidiaz bundling in Andy Polo’s cross from the byline, appearing to use his arm to score.
Ruidiaz wheeled away to celebrate and Uruguayan referee Andres Cunha signalled for the goal.
Brazil’s players protested furiously, swarming around Cunha in a bid to persuade the official to reverse his decision.
The protests appeared to have swayed Cunha, who looked to be frantically checking with the fourth official via his headpiece to determine whether the goal should stand.
Yet after a chaotic delay of around four minutes — with players from both sides surrounding the referee — Cunha ruled the goal should stand.
Multiple replays from different angles however showed that Ruidiaz had used his arm to knock the ball into the net past Brazil keeper Alisson.
Brazil pressed forward in the final closing minutes in an attempt to find an equaliser that would take them through but it was to no avail.
Elias squandered a golden chance from close range in injury time and Brazil’s fate was sealed.
Peru will now play Colombia in the quarter-finals after finishing top of Group B.
Despite the controversial nature of the result, Brazil coach Dunga is likely to come under renewed pressure after what was a lacklustre tournament.
The Brazilians, desperate to begin the road to recovery after their humiliating 7-1 defeat by Germany in the World Cup semi-finals two years ago, never convinced at any point of the group stage.
A 0-0 opening draw with Ecuador — where a refereeing decision which might have given Ecuador a victory went in Brazil’s favour — was followed by a 7-1 drubbing of Haiti, arguably the weakest team in the tournament.
Another drab display against Peru followed on Sunday, with Dunga’s side barely getting a shot on goal in the first-half with the exception of a shot by Gabriel in the 26th minute that was tipped away by Peru goalkeeper Pedro Gallese.