Karim Benzema had swept the champions into the lead after just 90 seconds but Jose Mourinho's decision to once again leave Iker Casillas on the bench backfired just six minutes in as Antonio Adan brought down Carlos Vela inside the area and was rightly sent-off.
Xabi Prieto slotted home the penalty on what was to be a memorable afternoon for him and both sides exchanged further goals before half-time as Sami Khedira put Madrid back in front only for Prieto to level again five minutes later.
Cristiano Ronaldo then looked to have sealed the points with two goals in two minutes midway through the second-half, but Sociedad were given another lifeline when Prieto completed his hat-trick with 15 minutes remaining.
However, the visitors themselves were reduced to 10 men on 80 minutes as Dani Estrada was shown a second yellow card for bringing down Ronaldo and the Basques failed to muster another serious effort on goal.
The pre-match talk had been dominated by the debate over who would start in goal for the hosts and Mourinho was left in no doubt as to the fans opinion as his name was roundly booed and that of Casillas cheered when the teams were read out, but those the Portuguese had chosen to play got off to the perfect start as Khedira fed Benzema who calmly slotted home his 10th goal of the season.
The real drama arrived just four minutes later as Adan gifted the ball to Vela just outside the area before bringing down the Mexican as he tried to round him. Referee Ignacio Iglesias Villanueva had no option but to award the penalty and send Adan off meaning Casillas was forced into action at the expense of Jose Maria Callejon.
There was nothing the Spain captain could do to prevent Prieto levelling the scores, though, as he calmly slotted his penalty high down the middle of the goal, and Casillas then nearly made a costly error himself as he presented Prieto with a great chance to put Sociedad in front with a slack kick out, but this time the midfielder put his effort into the sidenetting.
The visitors then enjoyed a prolonged period of possession but Vela's long-range effort that was comfortably handled by Casillas was their only threat on goal and they were punished for their inability to take a firm grip on the game as Madrid regained the lead 10 minutes before half-time. A corner from the right fell to Ricardo Carvalho and his misfit effort was brilliantly backheeled past Bravo by Khedira.
Again, though, the lead only lasted five minutes as a strike from distance by Vela deflected off Xabi Alonso perfectly into Prieto's path who swept home from close range.
The hosts went agonisingly close to going ahead again before the break as rapid counter-attack from a Sociedad corner saw Mesut Ozil feed Benzema and after his effort rebounded back off the inside of the post, Khedira skewed wide with the goal at his mercy.
Bravo then had to be alert to divert a dipping effort by Ronaldo in the first action of the second period before the Portugal captain saw another shot drift just wide.
However, Ronaldo was to prove decisive as he latched onto Benzema's pass over the top to slot home 22 minutes from time and then sealed all three points with a free-kick from the edge the area which Bravo somehow failed to tip over the bar.
Gonzalo Higuain then came within inches of marking his return from injury with a goal as he struck the bar from Ozil's through ball with virtually his first touch, but Sociedad were brought back into the game again as Prieto completed his hat-trick with a fine angled drive that left Casillas stranded.
The numbers were levelled for the final 10 minutes as Dani Estrada saw a second yellow card for bringing down Ronaldo and Madrid saw the game out.