Alvaro Morata's late header lifted Juventus into the UEFA Champions League knockout stages as Andrea Pirlo's side claimed a last-gasp 2-1 win over Ferencvaros.
It looked as though Juve would fall five points behind Group G leaders Barcelona as they toiled in Turin, with Ferencvaros' goalkeeper Denes Dibusz having made a string of fine stops.
Dibusz had been beaten by Cristiano Ronaldo's thumping 35th-minute equaliser, which cancelled out Myrto Uzuni's opener and moved the 35-year-old level with Lionel Messi on 70 home Champions League goals.
Yet it was an error from Dibusz – who made several mistakes in the reverse outing – which ultimately cost Ferencvaros a point, the goalkeeper only able to fumble Morata's header over the line in the 92nd minute.
Paulo Dybala volleyed at Dibusz with a quarter of an hour gone and Juve fell behind four minutes later – Uzuni tucking into a gaping goal after Tokmac Nguen had capitalised on Matthijs de Ligt's mistake.
Juve soon hit back and, two minutes after he saw a strike disallowed for offside, Ronaldo equalled Messi's haul of home goals in UEFA's premier competition, lashing a strike into the bottom-right corner from the edge of the area.
Ronaldo almost had his second early in the second half, but Dibusz beat him to a poor back-pass.
Dibusz pulled off another vital stop on the hour, tipping Federico Bernardeschi's venomous strike onto the upright before he then denied Ronaldo a tap-in with a well-timed dive at the forward's feet.
The post came to Dibusz's rescue when Morata curled a close-range strike beyond him, but another fine stop from Rodrigo Bentancur's effort seemed to have sealed a point.
It was not to be, though, with Dibusz failing to keep out a Morata header which was directed straight at him as Juve clinched their last-16 place with two games to spare.