La Liga Wrap: Rayo, Malaga pick up away wins
In an action-packed affair at Anoeta, the hosts took the lead after just two minutes when Inigo Martinez headed home a dangerous inswinging delivery from Ruben Pardo.
Their lead was shortlived as Joaquin Larrivey equalised from the spot just two minutes later, but Pardo was on hand again to help Sociedad into a half-time lead - his shot taking a deflection on the way to finding the bottom corner on the stroke of the interval.
Rayo striker Alberto Bueno then found the net for the first time since January 12 two minutes after the restart, before Ruben Rochina scored the winner with a fierce left-footed drive on 67 minutes.
The victory puts 19th-placed Rayo level on points with Almeria and Real Valladolid, as Sociedad missed the chance to move up to fifth.
Earlier on Monday, Malaga also bolstered their chances of beating the drop as they saw off Osasuna with a polished performance, even after seeing Jesus Gamez sent off early in the second half.
Samu opened the scoring for the visitors after just 14 minutes, producing an impressive finish on the half-volley to beat Andres Fernandez.
Malaga were given a scare when Gamez picked up two yellow cards either side of the interval, although any fears were allayed when Nordin Amrabat finished well six minutes after the hour.
Roque Santa Cruz slipped in the Galatasaray loanee and the forward made no mistake - firing past Fernandez to help his side move above Osasuna and three points clear of the relegation zone.