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 giants had returned winless from their last seven trips to Sociedad in the top flight and were primed for further disappointment after falling behind to Willian Jose's 11th-minute header.
Juanmi doubled the hosts' 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 them 19 ahead of champions 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 visitors found themselves 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 their play uncharacteristically sloppy and Sociedad looking the more confident team on the ball.
The home side thought they 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.
Their disappointment was soon assuaged as another goal did arrive, Juanmi collecting a wonderful lofted throughball 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.
But 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 their 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.
New record for Lionel Messi!— Omnisport (@OmnisportNews) January 14, 2018
The Argentina star's 366th league goal for Barcelona takes him beyond Gerd Muller, who previously held the record for most goals for a single club in Europe's top five divisions.
Barca win 4-2 at Real Sociedad.#LaLiga #FCBarcelona pic.twitter.com/kErueJjXTb