The home side were two goals down early in the first half but roared their way to an astounding victory against the European champions
The European champions seemed set for a comfortable night when early goals from Sergio Ramos and Gareth Bale left the hosts shell-shocked.
But Sociedad regrouped outstandingly, with goals from Inigo Martinez and Carlos Vela complementing David Zurutuza's double to secure a famous triumph.
Questions have surrounded Carlo Ancelotti's Madrid side in 2014-15 so far after an uninspiring 2-0 win over Cordoba last Monday followed their Supercopa de Espana defeat to Atletico Madrid.
Those results supported claims that the capital club have lost their balance following the departures of Angel di Maria and Xabi Alonso - now this dreadful defensive showing on Sunday will only strengthen the arguments.
Initially, it did not seem those frailties would matter as the visitors, despite being without Cristiano Ronaldo, attacked with menace and created chances at will.
Madrid flew out of the blocks and took an early lead when Ramos rose to meet Toni Kroos' corner with a glancing header at the front post.
That set the tone for the rampant European champions, Ramos almost grabbing a second with a stunning free kick that rattled the crossbar.
And Madrid did double their advantage in the 11th minute as Bale brilliantly nutmegged Gorka Elustondo and slotted a cool finish into the corner.
Not content with being two up, they continued to press forward only to see Marcelo twice spurn good chances before Ramos headed just over from a James Rodriguez set-piece.
The defender was pressed into action in his own box soon after, bravely flinging himself in front of Esteban Granero's powerful, goal-bound effort.
Normal service was resumed moments later as Enaut Zubikarai produced a fine stop with his leg to deny Toni Kroos.
The catalogue of missed chances looked costly in the 35th minute, Martinez sneaking in at the back post to convert after Xabi Prieto had flicked on a perfectly-placed corner.
And the scores were level six minutes later as Zurutuza arrived late into the box to powerfully head home Alberto de la Bella's centre.
Goals did not bring a defensive improvement for Sociedad, with Ramos getting on the end of another free kick and heading agonisingly wide.
Madrid's defending was not much better, illustrated at the start of the second half when an unmarked Imanol Agirretxe saw a header just turned wide by the toe of Iker Casillas.
But the turnaround was complete in the 65th minute as Prieto and Sergio Canales played a neat one-two that ended when the former slid a lovely ball across for Zurutuza to slot home.
The verve with which Ancelotti's side attacked early on seemed a distant memory as they struggled for cutting edge.
Yet it was the issues at the other end that were most troubling, their inability to clear a set-piece proving costly when Vela followed a fine piece of chest control with a volley that arrowed into the top corner as defenders appealed for handball.
Sami Khedira, Rodriguez and Karim Benzema all missed golden opportunities thereafter as the visitors failed to find a route back into the game.