Diaz made the most of his latest audition by opening the scoring for Madrid early on as he jinked past a couple of players and then had the composure to convert past Geronimo Rulli from an angle.
Sociedad responded well to that early setback, coming close through on-target attempts from Merino and Mikel Oyarzabal, the latter turning on the edge of the box and unleashing a powerful half-volley for Thibaut Courtois to deal with.
The hosts had a goal ruled out a quarter of the way into the match as, after more than four minutes of VAR checks, Oyarzabal was deemed to be offside in the first phase of play prior to converting at the back post.
Seconds after play resumed, Sociedad were back on level terms thanks to a well-worked goal that culminated in Willian Jose flicking the ball in behind for Merino to convert.
Isco had a shot from 12 yards kept out, but it was the home team who were looking the more positive as Willian Jose's header was cleared off the line by the head of Daniel Carvajal, before Vallejo did likewise, albeit with the use of his hand.
That led to the referee sending off the centre-back and awarding a penalty, which Courtois was equal to down the middle - Willian Jose opting for power over precision.
Madrid refused to sit back despite the man disadvantage and they were punished for that in the second half.
Igor Zubeldia's curler forced Courtois into a good save and Merino sent another effort wide, but the second Sociedad goal arrived just short of the hour mark through Zaldua's looping header.
The full-back, previously without a goal in senior football, outjumped Marcelo at the back post and was given a huge helping hand by Courtois, who slipped and could only watch the ball drop under the crossbar.
Barrenetxea killed off the match by converting from inside the box after Oyarzabal got between Carvajal and Marcelo and picked out the frame of the goal, setting up his teammate for a simple finish from the rebound.
Sociedad came close to scoring the fifth goal of the match, with substitute Adnan Januzaj smashing the ball against the crossbar in the final 10 minutes.
Madrid have a chance to return to winning ways with a home match against Real Betis in their final game of the season next Saturday, while Sociedad - still in the hunt for a Europa League spot after this win - travel to Espanyol on the same day.
REAL SOCIEDAD (4-3-3): Rulli; Zaldua (Januzaj 68'), Moreno, Llorente, Munoz; Pardo (Illarramendi 77'), Zubeldia, Merino; Barrenetxea (Juanmi 87'), Willian Jose, Oyarzabal
REAL MADRID (4-3-3): Courtois; Carvajal, Nacho, Vallejo, Marcelo; Isco (Modric 56'), Casemiro, Kroos; Asensio (Vinicius 76'), Benzema, Diaz (Lucas 65') body check tags ::