Los Blancos' 3-2 success at Sporting Gijon earlier in the afternoon had moved Zinedine Zidane's side six points clear at the summit, but Barcelona are back to within three ahead of El Clasico at the Bernabeu next weekend.
Barcelona boss Luis Enrique made four changes to the team that started against Juventus during the week, with Jordi Alba, Sergio Busquets, Andre Gomes and Paco Alcacer all coming into the XI. Neymar missed out through suspension, however, while a calf problem kept Javier Mascherano on the sidelines.
Sociedad had a big chance to take the lead in the third minute when Willian Jose found Yuri Berchiche inside the Barcelona box, but the defender's goal-bound strike was blocked by the sliding Gerard Pique.
Barcelona had a super opportunity of their own two minutes later when Alba found space down the left before delivering a low cross into Luis Suarez, but the Uruguay international slipped at the vital moment.
The visitors continued to look dangerous, however, and Barcelona goalkeeper Marc-Andre ter Stegen had to keep out a low strike from Oyarzabal in the 12th minute after the youngster had found a lot of space inside the box.
The Catalan giants took the lead in the 17th minute following a moment of genius from Messi though, with the Argentine drilling an unstoppable attempt into the bottom corner after working some space in a dangerous position.
Messi almost doubled Barcelona's lead in the 20th minute after a clever pass from Luis Suarez found the attacker inside the Sociedad box, but smart goalkeeping from Geronimo Rulli kept the score at 1-0.
Alcacer was next to come close for the home side after Roberto had found the former Valencia striker in a dangerous position, but the Spaniard got his effort all wrong as Sociedad survived a dangerous moment.
The visitors were arguably the better team at Camp Nou during the opening 35 minutes, with Willian Jose, Carlos Vela and Oyarzabal continuing to cause problems in the final third of the field.
However, Messi doubled the home side's lead in the 37th minute when he converted from close range after Luis Suarez's effort had been saved straight into the path of the Argentine.
Sociedad were back in the game when Samuel Umtiti turned an Inigo Martinez cross into his own net for 2-1 in the 42nd minute, but Barcelona scored their third less than two minutes later when Messi released Alcacer inside the Sociedad box, and the centre-forward found the bottom corner with a smart finish.
That was not the end of the first-half action, however, with Xabi Prieto incredibly making it 3-2 in the first minute of stoppage time at the end of the first period when the Sociedad captain volleyed a wonderful Willian Jose pass into the bottom corner.
Sociedad also came out swinging at the start of the second period, with Willian Jose and Vela both finding space behind a Barcelona defence that continued to look vulnerable.
Messi tested Rulli with a long-range free kick in the 50th minute as Barcelona hit back, but it was always a comfortable save for the Argentine goalkeeper, who had a mixed game at Camp Nou.
The home side still had the lead approaching the final 30 minutes of action, but whistles from the Camp Nou faithful were an indication of what was occurring, with Sociedad pressing high up the field and causing problems on the counter-attack.
Willian Jose had a clear shooting chance in the 61st minute after finding all sorts of space inside the Barcelona box, but Busquets was across to make a vital challenge as the score remained 3-2.
Sergio Canales and Willian Jose both had half-chances for Sociedad as the second period gathered pace, with the Spanish champions struggling to put two passes together as the home supporters continued to air their frustrations.
The biggest cheer of the second half came in the 69th minute when Andres Iniesta replaced the ineffective Gomes, and the Spain international immediately boosted Barcelona, who started to enjoy more possession of the ball.
Sociedad came close to scoring a third on two separate occasions in the 72nd minute, but Ter Stegen tipped a long-range Asier Illarramendi effort over the crossbar, before Inigo Martinez headed a Canales corner wide of the post.
Barcelona did manage to stem the flow of pressure approaching the final five minutes, although the home side were still vulnerable to the counter-attack, especially with Jon Bautista providing fresh legs for the Basque outfit.
Luis Suarez tested Rulli with a close-range attempt late on, but the first-half score of 3-2 stood at the end of the 90 minutes as Barcelona kept their title hopes alive ahead of their visit to Real Madrid next weekend.
Next up for Barcelona is the second leg of their Champions League quarter-final at home to Juventus on Wednesday night, while Sociedad will attempt to return to winning ways in the league when they host Deportivo La Coruna next weekend.