Antoine Griezmann and David Zurutuza scored the winning goals for the hosts following Alex Song's own goal and the record-breaking Argentine's equaliser
An Alex Song own-goal 10 minutes from half-time was quickly cancelled out by a Lionel Messi goal that saw the Argentine surpass Raul's tally for Real Madrid and become the Spanish top flight's third top scorer in history, but things took a turn for the worse after the break for the Catalans.
Barca were undone by two strikes early in the second half, Griezmann scoring in the 54th minute after excellent work from Carlos Vela and David Zurutuza adding a third five minutes later.
Sociedad move up to fifth, while Barca, who saw coach Gerardo Martino sent to the stands at half-time, could drop to third if Atletico Madrid avoid defeat against Osasuna on Sunday.
It took 20 minutes for the first threatening moment of the match to arrive as Barca opened up La Real's defence through Andres Iniesta and Pedro.
The duo exchanged a number of passes before a chance opened up for Pedro, but he curled his effort wide of goal without testing Claudio Bravo.
Sociedad then made the breakthrough as Gorka Elustondo forced an error from Song. Sergio Canales delivered a pinpoint cross into the penalty area where Elustondo, who had got in front of the Cameroon international, saw his header deflect off Song's chest and fly past Victor Valdes.
Sociedad's lead only lasted four minutes as Messi, anonymous in the opening half-hour, levelled with a low drive from the edge of the penalty area.
After flicking the ball out to Martin Montoya, the Argentine met his return pass after Sergio Busquets had let it run through his legs and made no mistake with a clinical finish.
Both sides had chances to move ahead at the start of the second half. but it was Sociedad who got a second goal as Griezmann netted his 15th of the season in the league.
Vela was released behind the Barca defence and rolled the ball across the penalty area for Griezmann, who fired first time beyond Valdes.
Things soon got even better for the hosts as they extended their advantage, with Griezmann heavily involved again.
The Frenchman spotted the late run of Zurutuza into the penalty area and the midfielder flicked the ball past Valdes to leave Barca with a mountain to climb.
Even the introduction of Cesc Fabregas and Alexis Sanchez could not rouse Barca, who were lucky not to concede a fourth goal as Vela fired against the post in the final 10 minutes.