Sociedad had lost four of their last five matches, but they took the lead inside the opening 60 seconds when Carlos Vela found the bottom right corner with a low drive from eight yards.
Malaga levelled the scores eight minutes before half time, Javier Saviola tucking away from close range following an inswinging corner.
However, the visitors regained the lead on the hour-mark with what proved to be the winning goal.
Xabi Prieto found the net for the first time this season with a precise finish from seven yards.
The result moves Sociedad up to 13th, while Malaga remain fifth.