Huesca shocked the Bernabeu in to silence when they took a third minute lead. Ezequiel Avila won back possession before running towards the box and teeing up Cucho Hernandez, who fired past Luca Zidane for the opening goal.
Madrid nearly drew level in the 12th minute when left-back Marcelo found plenty of space down the left wing before crossing but Ceballos could only fire past the near post.
Three minutes later, Benzema came close after Alvaro Odriozola won possession back, but the Frenchman's shot on the turn was well saved by Roberto Santamario in the visitors' goal.
In the 25th minute, the hosts equalised. Diaz's pace caused chaos before he teed up Benzema, but the former Lyon man was denied by Santamaria. However, the rebound fell to back to Diaz who squared the ball for Isco for a simple tap in from close range.
Just before the half-time whistle, Huesca had a good opportunity to restore their lead. A deflected cross from the left wing fell kindly for Avila, who turned on to his left foot and shot but it sailed over the crossbar.
Los Blancos almost took the lead two minutes after the interval, but Benzema was denied by a smart save from Santamaria with his shot looking destined for the bottom of the corner.
Zinedine Zidane's side went in front in the 62nd minute. Gareth Bale picked out Benzema at the far post with a looping cross before the striker headed back across goal for Ceballos to volley home under pressure.
Bale then missed a sitter which would have made it 3-1. Odriozola managed to find space before drilling a cross towards the near post and Bale somehow missed from inside the six-yard box.
Huesca equalised in the 74th minute. From a short corner, Moi Gomez had plenty of time to float his cross to the near post before Xabier Etxeita steered the ball past Zidane.
A moment of magic from Benzema in the 89th minute secured the three points for Zidane's men. Benzema found a pocket of space inside the box after Marcelo laid the ball off to him and he curled a stunning strike in to the top left-hand corner to spare Madrid blushes.