Madrid had looked set to sweep Sociedad aside as they struck twice early on through Sergio Ramos and Gareth Bale while also hitting the woodwork through the Spain defender.
The visitors also had a number of other chances to increase their lead before Sociedad staged a stunning comeback that saw them snatch the victory through goals from Inigo Martinez (36), David Zurutuza (41 and 65) and Carlos Vela (76).
That earned Sociedad their first points of the new season and came at the end of a week which saw them beaten by top-flight newcomers Eibar last weekend and knocked out of the Europa League on Thursday by Krasnodar
Those results increased the pressure on coach Jagoba Arrasate and he must have been wondering about his future as Madrid made a blistering start to Sunday's clash at the Anoeta.
Carlo Ancelotti's men, who were far from impressive in beating minnows Cordoba at home last week, were without the injured Cristiano Ronaldo and saw Angel di Maria and Xabi Alonso sold this week, but they could hardly have been more impressive in the opening stages.
There were just five minutes on the clock when Ramos got on the end of Toni Kroos' inswinging corner to send a glancing header in off the far post, and he came within inches of making it 2-0 four minutes later with a brilliant free-kick that came back off the bar.
Madrid need not have worried though as moments later Bale did double their lead, the world's most expensive player picking up a pass from Luka Modric before nutmegging Gorka Elustondo and drilling a low shot inside the near post.
Rampant Madrid had further opportunities to increase their lead with Ramos heading over and Marcelo and Kroos being denied by brilliant saves from home keeper Enaut Zubikarai.
At the other end, Sociedad had produced little to worry Madrid in the opening half-hour save for a thunderous shot from Esteban Granero that was superbly blocked by the brilliant Ramos.
However, the hosts got a foothold back into the match when Martinez escaped Ramos at the far post to slot home from a corner, and then five minutes later the Basque side were back on terms when Zurutuza thundered home a header from near the penalty spot.
Madrid were shellshocked but they still almost re-took the lead before the break as Ramos bravely beat two defenders and the goalkeeper to a James Rodriguez free-kick only to see his header go inches wide.
The second half proved one to forget for Ancelotti's all-stars as Sociedad scored twice in the space of 11 minutes to take the win.
Good work between Xabi Prieto and Sergio Canales saw Madrid opened up down the right and the former's low cross was fired into the roof of the net by the sliding Zurutuza.
Madrid barely had a chance to draw level before they found themselves 4-2 down in controversial fashion, the visitors claiming Vela had brought the ball down with his arm before lashing a left-footed shot past Casillas.
Madrid had opportunities to set up a frantic finale but Sami Khedia headed over, Zubikarai denied Rodriguez and Karim Benzema hit the outside of the post as they left San Sebastian empty-handed.