Sociedad led 2-0 against the league leaders after 34 minutes of football, but Paulinho netted late in the first period before a second-half double from Luis Suarez and a late Lionel Messi effort secured all the points for Ernesto Valverde's side.
The victory was Barcelona's first in the league at the Anoeta for over 10 years, and it has moved them nine points clear of second-placed Atletico Madrid. Champions Real Madrid, meanwhile, are now 19 points off the pace in Spain's top flight.
Inigo Martinez was a notable absentee for the home side as the speculation surrounding the centre-back's future continues. Martinez's omission allowed Kevin Rodrigues to start, while Juanmi replaced Mikel Oyarzabal in a wide position.
It was Messi that had the game's first attempt in the ninth minute, but the Argentine saw his effort just miss the crossbar as both teams enjoyed the ball in what was an open start to the match.
Sociedad were having joy down the right, and they took the lead from that avenue in the 11th minute when Willian Jose's header from a Xabi Prieto cross hit the underside of the crossbar before landing into the back of the net.
Paulinho had a decent chance to answer back for Barcelona three minutes later, but the Brazilian saw his goal-bound strike blocked before eventually conceding a free kick in an attempt to regain the ball.
Sociedad thought that they had scored their second just before the 30-minute mark when Willian Jose's deflected effort ended up in the bottom corner, but the referee had blown his whistle for a free kick after Ivan Rakitic tumbled.
Barcelona were untidy at the back, and Sociedad did indeed double their advantage in the 34th minute when Juanmi found space inside the away side's box before poking into the back of the net - albeit with the help of a deflection off Sergi Roberto.
The league leaders halved the deficit just five minutes later though when Paulinho slid a low Luis Suarez cross into the back of the net from close range. Sociedad goalkeeper Geronimo Rulli got a hand on the ball, but could not keep it out.
Canales had the first chance of the second period after finding space outside the Barcelona box, but the number 16 could only strike into the arms of the waiting Ter Stegen.
Barcelona were level in the 50th minute, however, when Luis Suarez curled a spectacular effort into the top corner. The ball looked to be floating wide before ultimately finding the back of the net.
Sociedad made two changes in the 10-minute period that followed, but it was Barcelona in control of events as the league leaders looked to complete the turnaround at the Anoeta.
Dembele came off the Barcelona bench in the 65th minute during an open period of the match as the rainy conditions continued to produce moments of panic inside both penalty boxes.
Barcelona led for the first time in the match in the 71st minute when Luis Suarez took advantage of some poor defending from the home side to slide a low finish past Rulli, whose loose clearance had caused the initial issue. Thomas Vermaelen rose highest to head towards the Uruguay international, and he did the rest.
Messi brought a fine save from Rulli in the 81st minute as Barcelona looked for a fourth to seal all the points, and that finally arrived in the 85th minute when Messi floated a free kick into the top corner of the net.
SOCIEDAD (4-3-3): Rulli; Odriozola, Llorente, Navas, Rodrigues; Zurutuza (Zubeldia 56'), Illarramendi, Prieto (Januzaj 78'); Juanmi (Oyarzabal 60'), Willian, Canales
BARCELONA (4-1-2-1-2): Ter Stegen; Roberto, Pique, Vermaelen, Alba; Busquets, Rakitic, Gomes (Digne 80'), Paulinho (Dembele 65'); Messi, L.Suarez (D.Suarez 90')