The star forward opened the scoring early in the second half at the Samara Arena and it was his shot being saved which led to Roberto Firmino's 88th-minute clincher.
Neymar scored a 51st minute goal to put Brazil up 1-0 against Mexico.
It was Brazil's 227th goal at the World Cup, meaning they overtake Germany as the all-time top scorers.
Meanwhile, Layun was also lucky to escape a red card after stamping on Neymar's ankle 19 minutes from time, but the referee decided not to take any action against the former Watford defender. Hello and welcome to the Round of 16 match between five-time champions Brazil and Mexico.
Buoyed by that sorely-needed injection of dynamism, Tite's men then carved out two quick-fire chances.
Jesus went close again in the 33rd minute, finding space in a crowded area and drilling in a left-footed strike that Ochoa palmed away.More news: Priyanka Chopra leaves India for United States with Nick Jonas in tow
More news: Suarez Says Rivalry With Ronaldo Different When Playing for Uruguay, Barcelona
More news: Lukaku ties with Ronaldo in race for World Cup Golden Boot
After the restart, both teams showed their intent to break the deadlock with Philippe Coutinho being superbly denied by Ochoa from 12 yards and Mexico's Jesus Gallardo advancing from the halfway line before curling over the crossbar from distance.
He darted across the edge of the area before a flawless backheel released Willian, whose low cross fizzed into the danger area where Neymar slid in to turn the ball home.
Paulinho nearly doubled the lead before the hour, but his shot inside the box was kept out by the excellent Ochoa, who then had to tip Willian's fizzing drive over the bar.
Neymar powered through and his low effort was diverted into the path of Firmino, who had the simple task of tapping into an empty net.
Neymar scored the opening goal to help ensure Brazil's Russian Federation 2018 journey continued.
After their slow start it was ultimately no more than the Selecao deserved, as their bid for a sixth world title marches on.