Barca were two goals ahead and had missed chances to add a third, before a fine fightback from the home side ended with substitute Imanol Aguirrete scoring the winning goal in injury time Saturday, Xinhua reports.
Barca were not helped by having to play the last 35 minutes with a player less after Gerard Pique was sent off for two yellow cards, and paid the price for a lack of defensive tension throughout the game played in San Sebastian.
Barcelona wasted little time in taking the lead as Lionel Messi cut inside and curled home a left foot shot. It was the tenth consecutive league game in which he has scored, equalling a record set by former Barca and Real Madrid striker Ronaldo.
The game was only six minutes old when Messi opened the scoring and he should have scored even earlier after wasting a chance when put clean through on goal only to drag his side wide.
Five minutes later, Pedro Rodriguez hit a curling shot against the Real Sociedad post with Claudio Bravo well beaten, but he was on hand to make it 2-0 midway through the half after Xavi Hernandez's pass set up Dani Alves to slide the ball across the area needing just a touch to send it into the net.
More From This Section
Messi could and should have scored a third when his chip from a ball bounced back off the inside of the post with seven minutes left in the half and from a possible 3-0, it became 2-1 when Chori Castro was given space on the left to hit a shot across Victor Valdes.
If coach Tito Vilanova spoke to his players at halftime, however, the message didn't get home and Pique earning himself a second yellow card 10 minutes after the break with a needless foul.
Javier Mascherano into the game, but one of his first actions was to deflect another Castro pass past Valdes to level the score.
The rest of the game was tense as both sides looked for the win in a tactical battle, but another defensive lapse in injury time allowed Aguirrete in between two Barca defenders to poke the ball past Valdes and send the home fans wild.