after six minutes with Pedro Rodriguez doubling the lead.
But Chory Castro pulled a goal back before the break and then got his second after 63 minutes.
With Gerard Pique off the field after receiving a red card, Agirretxe got the winner in the 91st minute.
"I have to congratulate Real Sociedad and I have no complaints about my players. We hit the post twice in the first half and had two other chances. It became more complicated for us and they made it 2-1," said Barca coach Tito Vilanova.
"I told Gerard (Pique) after he was booked that after the next foul he committed they would send him off and that's what happened. We were in control in the first half and although it looked difficult we were playing well. At 3-0 it would have been all over but the sending off changed everything."
There appears to be no record out of reach for Messi, who did score in 10 straight league matches that he was involved in during the 2011-12 season, notching up 18 goals in the process.
But now he has scored in Barca's last 10 and he equals the mark set by Ronaldo, who scored 12 in 10 games during the 1996-97 season, and Mariano Martin, who put away 18 in 10 matches between 1942 and 1943.
Messi has now hit 15 goals in 10 games and 29 overall in the league this season, but it counted for little on the night.
Barca went into the game having set a new record for a La Liga start with 18 wins -- and just one draw against Real Madrid -- for 55 points from a possible 57.
Despite the loss, they are still 11 points ahead of Atletico Madrid, who play Levante on Sunday, and Real Madrid in third, who face Valencia, a huge 18 points off the pace.
Barca once again began with Andres Iniesta in attack alongside Messi and Pedro with Alexis Sanchez struggling for form and David Villa injured.
Real Sociedad played the more defensive Markel Bergara in midfield rather than Ruben Pardo but even so they were under threat right from the start with a ball over their backline from Iniesta that saw Messi scamper clear on goal.
On this occasion, he slotted the ball the wrong side of the post but he was not so forgiving minutes later as he fired Barca ahead.
Iniesta was once again involved as he slipped the ball to Messi on the edge of the area and he picked his spot in the corner past the desperate dive of keeper Claudio Bravo.
A weakened Barca had drawn 2-2 with Malaga at Camp Nou in the first leg of the Spanish Cup quarter-final in midweek but they came out all guns blazing in San Sebastian.
Still Real have a dangerous forward line of their own and they went close to equalising after 10 minutes when Carlos Vela knocked a free-kick to Xabi Prieto, who had been left unmarked in the box, but he hit the side netting.
Real were refusing to sit back and defend and wait for their chances on the counter as so many sides do against Barca but it meant they left themselves open at the back and Pedro hit the post with a 20-yard strike on his weaker right foot.
The Canary Islander did extend Barca's lead midway through the half as he slid in to knock home a Dani Alves cross from close range.
Barca were again denied by the post after Messi had lobbed the ball over the keeper and then Real got back into the game through Castro, who fired in a clinical drive from the left of the area after being set up by Vela.
After the break Real managed to ruffle Barca with Pique receiving a second yellow card and then Castro got his second of the night.
Real were pressing forward and an Asier Illarramendi deep cross found Castro whose shot was deflected in off Javier Mascherano.
There was no late rally this time as has so often happened this season from Barca and instead they conceded at the death as Agirretxe scored from close range.
Malaga's hopes of a top four finish suffered a further blow as they could only draw 1-1 at home to Celta Vigo while new Sevilla coach Unai Emery saw his side draw 1-1 away to Getafe.