Juventus overcame a scare against Ferencvaros to qualify for the Champions League knockout stages thanks to Alvaro Morata’s stoppage-time winner.
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 sits second in Group C, and remain three points behind Barcelona.