Inigo Martinez put the hosts ahead with less than a minute played but Joaquin Larrivey quickly drew level from the penalty spot.
Ruben Pardo put La Real back in charge just before half-time with a somewhat fortuitous goal, but Alberto Bueno equalised from close range at the start of the second half and Ruben Rochina hit a fine winner midway through the second period.
Rayo remain 19th in the Primera Division but move up to 26 points, level with 17th placed Valladolid and only four points behind 11th placed Celta.
La Real meanwhile missed out on an opportunity to leapfrog Villarreal, and stay sixth.
With fellow strugglers Malaga, Granada and Valladolid all winning, the pressure was on Rayo to follow last week's 1-0 win over Valencia with another victory.
But they were up against a Sociedad side 20 points and 13 league places better off than them, having beaten champions Barcelona in their last home game.
La Real got off to the perfect start, Ruben Pardo swinging in a free-kick for centre-back Martinez to head home.
But Rayo responded in the ideal manner, Larrivey racing into the box and winning a penalty when pushed to the ground by Jose Angel. The Argentinian got himself up and slammed the ball into the bottom corner to score for the second week in a row.
Carlos Vela came close to putting the home side back in front on three occasions in quick succession, narrowly missing the target each time, while Rochina squandered Rayo's only effort after their goal before the break.
Jagoba Arrasate's side had one further attempt before half-time and were rewarded for their persistence as Pardo found his way through the Rayo defence and his deflected shot wrongfooted goalkeeper Ruben Martinez.
But as with the first goal, Paco Jemez's men had an immediate response. La Real's stand-in goalkeeper Enaut Zubikarai failed to gather a cross into the box, the ball bounced off Larrivey and was then bundled in by Bueno after 47 minutes.
Larrivey then had the ball in the net for the second time of the night a few moments later, although he was correctly ruled offside.
But Rayo had got a taste for scoring and were keen to do so again. Zubikarai redeemed himself for the blunder that led to the second equaliser by palming away a goalbound shot from Bueno.
But he could do little about Rochina's goal on 67 minutes, the Blackburn loanee playing a one-two with Bueno on the edge of the area then firing into the bottom corner once inside the area.
Once they went ahead, Rayo showed little sign of letting their lead go and were in control for most of what remained of the match, their only worrying moment coming when Vela entered the box with two minutes remaining, but the Mexican hesitated and the ball was cleared by the Rayo defence.