Brazilian striker Willian Jose gave the visitors the lead from close range in the 27th minute but Barca came right back at them, levelling four minutes later when Jordi Alba smashed the ball into the top corner with his weaker right foot.
The Catalans took the lead shortly before halftime when Frenkie de Jong arrived into the box and poked home a cross from Alba, initially seeing the goal ruled out for offside but then celebrating when it was awarded after a VAR review.
Real Sociedad, missing David Silva and top scorer Mikel Oyarzabal, failed to rise to the occasion, yet they should have equalised several times, such as when Alexander Isak was thwarted by a fine tackle from Barca's Pedri and a great save by Marc-Andre ter Stegen from close range.
"It was our best performance of the season, I'm so happy with the attitude the team showed," said Alba.
"It's difficult when you don't have fans in the stadium but we were still really motivated to win the game. Today we saw a great Barca side, one that was ready to fight for every ball."
A second consecutive victory took Barca up to fifth in La Liga on 20 points after 12 games and knocked Real Sociedad off the top and into second.
Atletico Madrid, who also have 26 points, sneaked up to the summit on goal difference but have one game in hand on Barca and three on Real Sociedad.
Barca produced a commanding performance, especially in the first half, but went behind against the run of play when Sociedad staged an elaborate move following a corner and Willian Jose appeared at the far post to nudge in a cross from Portu.
They quickly got back into the game through Alba's thumping strike and should have gone ahead when Antoine Griezmann hit the woodwork against the side where he began his career before Martin Braithwaite lifted the ball over the bar with the goal gaping.
They kept at it, though, and were rewarded when Dutch midfielder De Jong scored his first goal of the season.
Griezmann missed an even clearer chance to score in the second half, while Lionel Messi continued to show signs of a lack of sharpness.
Barca faced a nervy ending but pulled off a win that will lift their morale and revitalise their hopes of joining the title race.