Willian Jose gave Sociedad - who had lost their previous four games - an early lead from the spot, and after Lucas Vazquez was dismissed for two yellow cards the visitors eventually wrapped up the points thanks to a late second from Ruben Pardo.
The result leaves Real Madrid in fifth place in the standings, while Sociedad are now in 12th position after Imanol Alguacil marked the start of his second spell in charge of the club with a famous win.
Madrid went into the game knowing that they had to deliver a positive response to the draw with Villarreal on Thursday night, but Santiago Solari's side were behind as early as the third minute with Willian Jose firing a spot kick into the roof of the net after Casemiro had fouled Mikel Merino.
The home team soon went on the offensive and should have been level in the 10th minute through Karim Benzema, but the Frenchman somehow failed to hit the target from close range after the ball had fallen invitingly for him inside the six-yard box.
Vinicius Junior was impressing on his first La Liga start - although the youngster did fail to take advantage of getting in behind the Sociedad defence - while Luka Modric forced Geronimo Rulli into a smart save with a strike from 30 yards.
The visitors were frequently hitting Madrid on the counter but after openings failed to materialise, Solari's side went close again at the other end with Casemiro heading wide from a corner.
Madrid hit the woodwork just before the half-time whistle as Vazquez's miscued cross struck the far post before rebounding clear, but Sociedad deserved that bit of luck after matching the European champions for 45 minutes.
Sociedad went on the attack soon after the restart and probably should have doubled their lead through Mikel Oyarzabal, but the winger directed an effort over the bar from 14 yards after his initial touch had given Madrid a chance to apply some pressure.
Madrid went straight down the other end and Vinicius Junior cut inside before unleashing a shot from 20 yards out, but the Brazilian's effort curled marginally wide of the far post.
The contest soon descended into an end-to-end battle with any focus on defending seemingly out of the window and Adnan Januzaj was the next to try his luck for Sociedad, with the Belgian's left-footed curler from 20 yards going just wide of Thibaut Courtois's far post.
Madrid were dealt a significant blow just after the hour mark when Vazquez was dismissed for a second yellow card, and it left both sides to continue to push for a goal which could potentially prove to be decisive.
The home side should have found an equaliser soon afterwards, but after Vinicius Junior had seen a volley saved by Rulli at the back post Sergio Ramos sent the rebound straight at a defender who was standing in front of goal.
Januzaj also wasted another opportunity for Sociedad at the other end with the playmaker firing at Courtois from 12 yards out, but the visitors were not discouraged in their search for a game-clinching second.
More space was beginning to open up as the full-time whistle edged closer, and Sociedad finally sealed the three points in the 83rd minute when Willian Jose crossed for Pardo to power a header across Courtois and into the bottom corner.
Madrid continued to compete until the full-time whistle, but Sociedad eased through the closing stages to move six points clear of the relegation zone after just a fourth-ever top-flight win at the Bernabeu.
REAL MADRID (4-3-3): Courtois; Carvajal, Varane, Ramos, Marcelo (Reguilon 77'); Kroos (Ceballos 80'), Casemiro (Isco 57'), Modric; Vazquez, Benzema, Vinicius
REAL SOCIEDAD (4-5-1): Rulli; Elustondo (Zaldua 69'), Llorente, Moreno, Munoz (Zubeldia 84'); Januzaj, Merino (Pardo 73'), Illarramendi, Zurutuza, Oyarzabal, Willian Jose