Alvaro Morata scored a stoppage-time winner as Juventus overcame a scare against Ferencvaros to qualify for the Champions League knockout stages.
The Hungarian champions took a deserved lead when Myrto Uzuni got across his marker and poked home from nine yards.
Cristiano Ronaldo scored his 749th career goal as his left-foot drive from 20 yards found the bottom corner.
Juventus dominated the second half, but were set to be frustrated before Morata headed through Dénes Dibusz’s legs.
Andrea Pirlo’s side sit second in Group C, and remain three points behind Barcelona after Ronald Koeman’s side won 4-0 against Dynamo Kiev.