All looked to be going to plan for Tito Vilanova's visitors when Lionel Messi and Pedro Rodriguez put them 2-0 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 to clinch a dramatic 3-2 win.
Barca had headed to San Sebastian having dropped only two points in La Liga this season - in October's 2-2 Clasico draw with Real Madrid at the Nou Camp.
Second-placed Atletico Madrid could move within eight points of the leaders if they beat Levante on Sunday, while third-spotted champions Real Madrid can get within 15 points if they overcome Valencia at the Mestalla.
Sevilla failed to get Unai Emery's league reign off to the perfect start as an error from goalkeeper Andres Palop gifted Getafe a 1-1 draw at the Alfonso Perez Coliseum.
Jose Antonio Reyes put Sevilla in front with a neat finish shortly before half-time, but the lead lasted only three minutes as Palop got caught under Adrian Colunga's inswinging cross and allowed the ball to float over his head and into the net.
The late game also finished 1-1, with Malaga missing the chance to climb to fourth place as lowly Celta Vigo battled hard for a deserved point at La Rosaleda.
Martin Demichelis bundled the ball over the line after a goalmouth scramble to give the hosts the lead seven minutes before the interval, but Celta grew as the match wore on and levelled 14 minutes from time through Augusto Fernandez.
Granada secured only their second home win of the season as a Jordi Amat own goal and a late strike on his full debut from Recio handed them a much-needed 2-0 triumph over an under-strength Rayo Vallecano.
On Sunday, Atletico Madrid are in action at home to Levante and could cut the gap between leaders Barcelona to eight points after the latter's surprise loss to Real Sociedad.
Atletico boss Diego Simeone had some sobering words for fans of the club this week when he said he could not promise Radamel Falcao will remain with Los Colchoneros next season.
The in-form striker has been linked with a host of Europe's top clubs, and Simeone said supporters should just enjoy watching the Colombian while he is still at the club.
"We need to be realistic and I don't like to lie to the fans," he told Marca. "I don't know what can happen with Falcao and we need to enjoy him now he is with us. If he stays then it's much better, but I don't look so far ahead."
Real Madrid are in third place but could find themselves a staggering 18 points behind Barca when they kick off their match at Valencia on Sunday evening.
A goalless draw with Osasuna last time out has increased the pressure on coach Jose Mourinho, whose treatment of stalwart goalkeeper Iker Casillas continues to vex fans.
Antonio Adan's inclusion ahead of Casillas for recent matches against Malaga and Real Sociedad caused a furore on the terraces and in the Spanish press.
Adan, who could return to the starting XI this week after serving a suspension, believes he was wrongly targeted in the fallout, telling AS: "There wasn't just a lack of respect for what I have done in the youth teams, but there were opportunistic opinions. However, I try to be at the margins of all this."
Also on Sunday, Granada meet Rayo Vallecano, Osasuna play host to Deportivo La Coruna and Real Valladolid welcome Real Zaragoza, while Monday night's game sees Athletic Bilbaovisit Real Betis.
The action got under way on Friday night as Real Mallorca beat Espanyol 3-2.
Saturday's La Liga Results
Sunday's La Liga Fixtures
Friday's La Liga Fixture