All looked to be going to plan for Tito Vilanova's visitors when Lionel Messi and Pedro Rodriguez put them two goals up within 25 minutes.
But Gonzalo Castro's brilliant strike gave Sociedad hope before half-time and then, after Barca had lost centre-back Gerard Pique to a red card, Castro's curling effort was deflected into his own net by Barca substitute Javier Mascherano.
That sent the home fans wild, but it got even better in stoppage time when Imanol Agirretxe poked Carlos Martinez's cross from the right past Victor Valdes.
“We have to congratulate them. Their victory was achieved more because of their effort than because of our mistakes,” Barca coach Tito Vilanova said.
“I cannot reproach my players. In the first half we controlled the game and we hit the woodwork twice and another chance was saved by Bravo. We played against a team that had only allowed six goals at home.”