Brazil was controversially eliminated in the group stage of the Copa America Centenario after an apparent handball from Raul Ruidiaz lifted Peru to a 1-0 victory on Sunday.
Last year's semifinalist Peru booked a spot in the quarterfinals ahead of Brazil in Group B as Ruidiaz broke the deadlock with 15 minutes remaining but replays showed the substitute had used his hand to bundle Andy Polo's cross over the line as the game descended into chaos at Gillette Stadium in Foxborough, Massachusetts.
In farcical scenes, referee Andres Cunha awarded Ruidiaz's 75th-minute goal, only to then consult with his linesman and after almost five minutes of discussions amid protests from Brazil, which only needed a draw to advance, the decision was upheld.
Elias almost salvaged the required draw but the unmarked midfielder failed to score inside the six-yard box with three minutes to play.
After Ecuador's 4-0 rout of Haiti, the result meant Brazil failed to progress beyond the group stage for the first time since 1987.
After impressing in goal-scoring cameos against Haiti, teenage sensation Gabriel and Lucas Lima were both promoted to Brazil's starting XI at the expense of Jonas and the suspended Casemiro. A fit-gain Miranda returned to the lineup to partner Gil as Marquinhos dropped to the bench.
Peru also made three changes for the must-win clash, with Aldo Corzo, Adan Balbin and Andy Polo coming in for Renzo Revoredo, Renato Tapia and Alejandro Hohberg.
The first half ended all-square but it was not without its moments in Foxborough.
Uruguayan referee Andres Cunha waved away two penalty shouts from Brazil, with Peru fortunate to escape unpunished in the 24th minute after Luca Lima was clipped by Christian Ramos inside the area.
Brazil almost took the lead three minutes later via Gabriel after some nice build-up play from Elias, who almost picked out Dani Alves with a delightful ball minutes earlier, but the former's shot on the turn was turned away by Pedro Gallese's finger-tip save.
The Brazilians and their short and sharp interchange play continued to cause problems for Peru as Willian fired a shot just over the crossbar after sneaking in front of his opponent to connect with Filipe Luis' cross, while Gabriel's effort forced Gallese into another save four minutes before halftime.
While Brazil dominate possession, with Peru's all-time leading scorer Paolo Guerrero isolated up front, the side was also lucky not concede a penalty on the stroke of halftime after Edison Flores was tripped by Renato Augusto but the referee waved play on.
Peru emerged from the interval much improved, finally calling Alisson into his first save of the match following Cueva's 49th-minute free kick.
Against the run of play, the Peruvians stunned Brazil to open the scoring. Polo burst down the right-hand side, cutting in side before picking out Riduaz, who bundled the ball across the line, with replays showing he used his hand to score.
Brazil only required a goal to jump back into the box seat and Elias came within a whisker of sparing his team's blushes but he was unable to finish beyond Gallesse in the third minute of stoppage time as Peru held firm to seal their quarter-final berth.