The home side was 2-0 down early in the first half but roared its way to an astounding victory against the European champion.
Real Sociedad stormed back from two goals down to top Real Madrid 4-2, with a David Zurutuza brace leading the way.
The European champion seemed set for a comfortable night on Sunday when early goals from Sergio Ramos and Gareth Bale left the home side shell shocked.
Questions have surrounded Carlo Ancelotti's Real side in 2014-15 so far after an uninspiring 2-0 win over Cordoba last Monday followed the Supercopa de Espana defeat to Atletico Madrid.
Those results supported claims that Real has lost its balance following the departures of Angel di Maria and Xabi Alonso, and a dreadful defensive showing on Sunday will only strengthen the arguments.
Initially, it did not seem those frailties would matter as Real, despite being without the rested Cristiano Ronaldo, attacked with menace and created chances at will.
But having found itself 2-0 ahead after just 11 minutes, Ancelotti's men let Sociedad back into the game and were made to pay the heaviest of prices.
Real flew out of the blocks and took an early lead when Ramos rose to meet James Rodriguez's 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.
Real did double the advantage in the 11th minute as Bale brilliantly nutmegged Gorka Elustondo and slotted a cool finish into the corner.
Not content with being 2-0 up, Real continued to press forward - only to see Marcelo twice spurn good chances before Ramos headed just over from another 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.
The scores were level five minutes later as Zurutuza arrived late into the box to powerfully head home Alberto de la Bella's center.
Goals did not bring a defensive improvement for Sociedad, Ramos getting on the end of another free kick and heading agonizingly wide.
Real's defending was not much better, illustrated at the start of the second half when an unmarked Imanol Agirretxe saw a header turned wide by the toe of Iker Casillas.
The turnaround was complete in the 65th minute as Prieto and Sergio Canales played a one-two that ended when the former slid a lovely ball across for Zurutuza to slot home.
The verve with which Real 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, the team's inability to clear a set piece proving costly when Vela followed a fine piece of chest control with a volley that flew into the top corner.
Sami Khedira, Rodriguez and Karim Benzema all missed golden opportunities thereafter as Real failed to find a route back into the game.