In a chaotic match that saw two red cards and Casillas sprung from the bench, the capital outfit did just enough to survive Xabi Prieto's hat-trick to clinch a vital Liga victory
Jose Mourinho dropped Iker Casillas to the bench in favor of Antonio Adan for the second game running, and matters seemed to be going according to plan when Karim Benzema put the hosts in front after only two minutes.
However, it all fell apart for Madrid and Mourinho moments later when Adan conceded a penalty and was sent off for a professional foul, allowing Xabi Prieto to equalise from the spot. Sami Khedira would put los Blancos back in front, but Prieto pegged them back once more five minutes before the break.
The second half was just as eventful, as Cristiano Ronaldo appeared to put the match out of reach with a quickfire double with 20 minutes remaining, but Prieto made matters tense with his third goal, before Dani Estrada's late red card ended the challenge of Sociedad for good.
Despite the victory, however, Madrid still remain in third place, 13 points behind leaders Barcelona, who can stretch their advantage over second-placed Atletico at the top further when they face Espanyol at Camp Nou later on Sunday.
Mourinho’s decision to bench Casillas did not prove popular with the home support, who hailed the mention of the club captain while booing that of their coach during the pre-match line-up announcements.
The Portuguese trainer also shuffled his pack in defence, with Ricardo Carvalho and Raphael Varane stepping into the side in the absence of regular centre-back pairing Sergio Ramos and Pepe.
It took Madrid only two minutes to put themselves ahead, though, as Benzema latched onto an incisive through-ball from Khedira to finish confidently for the first goal of the match.
But six minutes in, things thing a dramatic turn for the worse for the hosts as a poor clearance from Adan invited pressure from Carlos Vela, who was then tripped up inside the area by the keeper, resulting in a straight red card for the latter.
The sight of Casillas readying himself on the touchline drove the fans into a frenzy, but after finally stepping between the posts after a lengthy delay, the club captain could do nothing to stop Prieto from converting the spot-kick.
Casillas nearly gifted Prieto a quickfire second with a dreadful pass out of his box, but the Sociedad skipper was unable to steer his first-time effort on target.
Madrid would edge back in front in the 35th minute when Khedira was on hand to turn in Carvalho's shot from the edge of the area.
But once again, their lead would not last long, as five minutes before half-time Vela’s shot was deflected right into the path of Prieto, who made no mistake from point-blank range.
Madrid had chances to regain the advantage just before the break. First, a long-range free kick from Ronaldo was beaten away with some flourish by Claudio Bravo. Then, with the last kick of the half, Benzema bent a great shot onto the post, with Khedira badly missing the rebound with the goal at his mercy.
In the 68th minute, los Blancos would finally surge ahead. A fantastic clipped ball from Benzema released Ronaldo, who needed only one touch to calmly place the ball into the back of the net.
And two minutes later, Ronaldo seemingly put the result beyond any doubt with a swerving free-kick from the corner of the box that flew into the roof of the net past the despairing palms of Bravo.
But Sociedad weren’t finished just yet, and Prieto ensured a nervy end to the game after latching on to Chori's through-ball to complete his hat-trick with 14 minutes remaining.
However, their hopes were put to rest once and for all in the 79th minute, as Estrada was shown his second yellow card for blocking Ronaldo on the wing, giving Madrid the boost they needed to close out the victory.