Ronaldo rescues Madrid on dramatic Casillas day
The Madrid captain was left out for the second consecutive game but was called upon in the opening minutes following a penalty conceded by his replacement Adan Garrido who was red carded.
Xabi Prieto, who was to grab a hat-trick, scored from the spot-kick to equalise after Karim Benzema had put Madrid ahead after just two minutes with Sami Khedira then restoring the lead for Jose Mourinho's side after 35 minutes.
Prieto struck again before half-time before Ronaldo came to the rescue with a brace after the restart.
Prieto hit his third with 14 minutes to go but Sociedad finished with 10 men also as Daniel Estrada was given a second yellow card.
Madrid are still 13 points behind Barcelona who play Espanyol later in a local derby and where they welcome back coach Tito Vilanova after cancer surgery.
Madrid faced an injury crisis at the back with Pepe, Fabio Coentrao, Marcelo and Raul Albiol all injured and Sergio Ramos suspended but they did have striker Gonzalo Higuain back after two months out and he began on the bench.
They got the perfect start with Khedira finding Benzema in space in the centre of the penalty area and he found the corner with a clinical finish.
But if the Bernabeu crowd were expecting to then see a comfortable victory after such an inconsistent season they were badly mistaken as only three minutes later Adan conceded the penalty for the visitors to draw level.
Adan's poor pass to Ricardo Carvalho went to Carlos Vela who bore down on goal and was upended by the shot-stopper. The referee pointed to the spot and also dismissed Adan which meant Casillas would play after all.
The player nicknamed 'Saint Iker' by Spain fans for his exploits in the recent successes of the national side was unable to make the perfect start as Prieto fired home the penalty.
An edgy looking Casillas moments later played the ball straight to Prieto who hit the side netting and then he came and failed to reach a corner ball crossed in.
Sociedad came into the game on the back of a strong run without a defeat in seven games and Antoine Griezmann went close with a couple of strikes as they played the ball around well in attack.
Madrid went back ahead with Khedira flicking in a shot by Carvalho from the edge of the area but they were never comfortable at the back and Prieto got his second when a Vela strike fell to him and he drove the ball past Casillas.
In first half stoppage time Benzema hit a post with fine effort from the edge of the area and Khedira was unable to direct the rebound goalwards.
After the break the game hit a lull before Ronaldo responded with a double. Benzema hit a ball over the top and he finished clinically and then a couple of minutes later he struck a free-kick into the top corner.
Sociedad didn't give up with Xabi Prieto latching onto a Chory Castro pass before beating Casillas but their hopes of an equaliser were hit by a red card for Estrada.
Related Real Madrid News