Philippe Montainier's side reversed the scoreline with two 90th minute goals to earn an incredible victory at San Sebastian over their strongly favoured visitors
A Martin Demichelis own goal gave the hosts the lead before Salomon Rondon and Seba Fernandez looked to have won it for the visitors.
That was until Arsenal loanee Vela's screamer levelled scores late on and substitute Ifran grabbed all three points at the death.
Despite the polarity of their league table positions, Sociedad gained a slight edge with through their quick counterattacks at the beginning of the match. They took advantage of this edge to open scoring.
Playing down the left, Daniel Estrada whipped in a brilliant cross which found Imanol Agirretxe who majestically headed the ball past Willy Cabalero but not the crossbar.
Bouncing back off of the woodwork, the ball deflected off of an unfortunate Demichelis and into the back of the net to open scoring in the 10th minute.
The away side did not take long to equalise. A free-kick found an unmarked Isco on the right and curled in a cross for Rondon who easily found the back of the goal with a header from only a few yards out.
From this point, the fixture featured a flurry of end-to-end action as both sides looked to catch their opponents with pacy counterattacks which gave the spectators what they wanted to see.
Antoine Griezmann troubled Manuel Pellegrini's men as his fine dribbling opened up space for David Zurutuza who riffled a strike at goal and forced a fine save out of Caballero just before half-time.
In the second-half, Malaga controlled the action, pressuring the home side high up the field and making it difficult for Philippe Montanier's men to escape their own half.
Duda threatened from range, but it was a horrid mistake from Gonzalez that gave Malaga the advantage. Thinking that he was alone in his 18-yard box, Gonzalez took his time in playing the ball.
With his back turned, he didn't see a surging Fernandez who stole the ball from the unfocused central defender and tapped it into the back of Claudio Bravo's goal.
The home side fought for the equaliser and earned it through a wonderful, last minute strike from Vela.
The Mexico international sent a roaring bicycle kick into the back of Caballero's goal after picking up on a flick-on inside the box.
The hosts were not finished and in dramatic fashion, Ifran grabbed the winner in the last seconds after finding space for himself between the centre-backs in the box and striking past Caballero once more.
With the victory, Real Sociedad move out of the relegation zone but a disappointed Malaga remain fifth and are left to wonder how they allowed their advantage to slip.