A stunning late free-kick at the end of an otherwise quiet outing from the Argentina star made him the top league goalscorer for one club in any of Europe's top five divisions, surpassing Gerd Muller with his 366th LaLiga goal.
The Catalan giant had returned winless from its last seven trips to Sociedad in the top flight and was primed for further disappointment after falling behind to Willian Jose's 11th-minute header.
Juanmi doubled the host's lead with a heavily deflected finish after a superb assist from Sergio Canales, but that only served to stir Barca into life and Paulinho reduced the deficit before half-time.
Within five minutes of the restart the scores were level as Luis Suarez finished brilliantly for his 12th league goal of the season.
Suarez then grabbed a second 19 minutes from time after goalkeeper Geronimo Rulli's poor clearance proved costly for Eusebio Sacristan's men, with Messi putting the icing on the cake.
The result restores Barca's nine-point lead at the summit, elevating it 19 ahead of champion Real Madrid, breaking an 11-year spell without a league win at Anoeta in the process.
Barca – without the services of injured new signing Philippe Coutinho – started brightly and Suarez was denied by the onrushing Rulli at the end of a determined run into the Sociedad box, while the keeper also had to be alert to keep out Sergi Roberto soon after.
But the visitor found itself behind as Willian Jose headed Xabi Prieto's searching cross into the ground and over Marc-Andre ter Stegen.
Barca struggled to mount any meaningful response, with its play uncharacteristically sloppy and Sociedad looking the more confident team on the ball.
The home side thought it had a second when Willian Jose's deflected shot nestled into the back of Ter Stegen's net, but Ivan Rakitic had been fouled beforehand.
Sociedad's disappointment was soon assuaged as Juanmi collected a wonderful lofted through-ball from Canales before driving his effort off Roberto and into the net.
Ernesto Valverde's men got back into the contest six minutes before the break, Suarez laying on a simple finish for Paulinho.
Barca threatened early in the second half, with Messi rounding compatriot Rulli, who recovered well to divert the ball behind for a corner that came to nothing.
Rulli was helpless to prevent Suarez's excellent equaliser, which he curled home side-footed from just inside the box.
Valverde introduced Ousmane Dembele in place of Paulinho just past the hour mark as he sought to get a firm grip on the contest, which was by then being played in sodden conditions.
Sociedad sent a timely reminder of its remaining threat when Willian Jose climbed well to connect with a header, but his attempt was wayward.
The turnaround was complete in the 71st minute, Suarez calmly tucking in his second after Rulli's lazy-looking clearance was headed into the Uruguayan's path by Thomas Vermaelen.
There was still time for Messi to create yet more history, sending a world-class free-kick into the net to make certain of the points.