Victory meant Barcelona re-established their nine-point lead which was temporarily reduced when second-placed Atletico Madrid defeated Eibar earlier on Saturday.
Sociedad were rewarded for their intelligent display when forward Juanmi equalised in the 62nd minute, sliding in to meet an incisive pass from Mikel Merino and diverting it past Barca's Marc-Andre ter Stegen.
They were not at their best and Philippe Coutinho was also paying for the excesses of last Tuesday.
"A goal before the break always helps because the game was at 0-0 and we were able to go ahead against a hard team with quality", he said.
Real Sociedad had not won at the Camp Nou since 1991 and they were soon trailing once more.
It's possible there will be changes [against Alaves] but we have to win our next game because the one after that catches us in the buiuld-up to the Champions League.
Diego Simeone's men - without suspended forwards Antoine Griezmann and Diego Costa - left it late but took the three points thanks to substitute Thomas Lemar's close-range finish in the 85th minute.
"This isn't over, not until we get to the last game is everything done".
"I have never seen a referee do that, I don't know what will happen".
Arturo Vidal went close three minutes later but could not keep the ball down when it was flicked on to him at the far post after a Messi free-kick.
However, the hosts grabbed the winning goal in the 69th minute as Aspas turned provider to set Sofiane Boufal for the goal. "We believe it. We have to play with a lot of personality and we are going to try it".
Meanwhile bottom club Huesca drew 0-0 with second from bottom Rayo Vallecano.