After Malaga had fought back from a goal down to take a 2-1 lead at the Anoeta, Carlos Vela struck in the 89th minute and Diego Ifran netted the winner two minutes later to lift Sociedad, coached by Frenchman Phillipe Montanier, to 13th.
Qatar-owned Malaga, one of Europe's biggest spenders in the close season transfer market, are fifth with 23 points, three behind Levante who were thrashed 5-0 at champions Barcelona on Saturday.
"It hurts to lose a game in the final three minutes," Malaga coach Manuel Pellegrini told a news conference.
"We had controlled this match," the Chilean, Jose Mourinho's predecessor at Real Madrid, added.
The future of Real Betis coach Pepe Mel hung in the balance after the Seville club conceded deep into stoppage-time to lose 2-1 at Osasuna, a ninth defeat in 10 matches that left them in 16th on 13 points.
"It's not up to me," Mel told a news conference when asked about his future.
"If they want me to carry on I'll carry on," he added. "If not I have a very clear conscience."
Erratic Atletico Madrid beat city rivals Rayo Vallecano 3-1 thanks to goals from Gabi, Radamel Falcao and Eduardo Salvio to climb to eighth.
Athletic Bilbao are a point behind in ninth after they drew 1-1 at Real Mallorca in a match littered with 11 yellow cards, while Granada climbed to 12th thanks to a 1-0 home win over bottom club Real Zaragoza in the late kick-off.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.