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).
Madrid had opportunities to set up a frantic finale but Sami Khedia headed over, Enaut Zubikarai denied James Rodriguez and Karim Benzema hit the outside of the post as they left San Sebastian empty-handed.
It meant Sociedad earned their first points of the new season 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.
Meanwhile, Sandro scored his first senior goal for Barcelona to help them to a narrow 1-0 win at Villarreal.
Luis Enrique's men had looked distinctly out of sorts at El Madrigal until the teenager's 82nd-minute goal, failing to zip the ball around in their usual manner.
They struggled to get too many sights of Villarreal's goal and only the woodwork meant they were not behind.
Sandro - the 19-year-old academy graduate - ensured they went home with the spoils, though, tapping in a cross from Lionel Messi who was frustrated by the Villarreal defence all night.
Elswhere, Isaac Cuenca kept his cool to slot home a stoppage-time penalty and salvage a 2-2 draw for Deportivo La Coruna at home to Rayo Vallecano.
Depor were beaten 2-1 in their season-opener at Granada last weekend after losing an early lead and it looked set to be an identical story on Sunday as Alberto Bueno's brace for Rayo cancelled out Jose Rodriguez's opener for the the hosts.
However, with three minutes of added time almost up, Depor won a penalty when Abdoulaye Ba handled in the box and Cuenca stepped up to earn the promoted side their first point since returning to the top flight.
Elche grabbed a 1-1 draw with Granada in a game that burst into life late on.
Both teams had chances in a goalless first half but the second looked to be petering out into stalemate, only for Fran Rico to give the visitors an 82nd-minute lead.
However, Elche found a response and David Lomban equalised at the death.