BARCELONA, Spain (AP) -- Ten-man Barcelona lost its first match in the Spanish league on Saturday, squandering a two-goal lead in a 3-2 defeat at Real Sociedad and conceding an injury-time winner.
Lionel Messi scored for a 14th straight start to open for Barcelona in the seventh minute, and Pedro Rodriguez appeared to give the visitors full control in the 25th.
But Gonzalo "Chori" Castro pulled one back in the 41st and equalized in the 63rd after Barcelona's Gerard Pique was dismissed for a second yellow card.
Imanol Agirretxe went on in the 84th as Sociedad looked to press home its numerical advantage, and the striker tapped in Carlos Martinez's last-gasp cross for the winning goal.
Barcelona had set a league record by winning 18 games and drawing only once by the competition's midway point.
"One day we were going to lose," Barcelona coach Tito Vilanova said. "Since it happened in the last minute I'm not worried. I don't have any reproach for my players. We hit the post twice and had several other chances in the first half. But not even with the 2-0 lead did I think it would be easy."
The Catalan club is still in command of the title race, 11 points ahead of Atletico Madrid before its game against Levante on Sunday. Defending champion Real Madrid is 18 points behind before Sunday's visit to Valencia.
For the second time in four days, Barcelona's defense has failed to protect a lead. On Wednesday, it let 10-man Malaga rally to score in stoppage time and snatch a 2-2 draw in the first leg of their Copa del Rey quarterfinal tie.
Saturday's game was similar to last season's meeting in San Sebastian between them. In both games Barcelona scored twice to apparently have the game in hand before Sociedad roared back. Last year the same fixture ended 2-2.
"We could have finished off the game, but it is always tough to win here," Barcelona midfielder Xavi Hernandez said.
Sociedad, which has lost only one of its last 10 games -- a 4-3 shootout at Madrid this month -- pressed up the pitch from the start.
But Barcelona looked set to extend its 12-game league winning run with Messi and Pedro each scoring and hitting the goal frame.
Messi struck with his first career goal at Anoeta Stadium and his league-leading 29th goal of the season through 20 matches.
Moments after a rare miss by Messi one-on-one with the goalkeeper, Andres Iniesta poached Claudio Bravo's short goal kick, crossed the ball for Cesc Fabregas, who deftly let it roll through to Messi, and the Argentina forward quickly put the ball on his left foot before unleashing a precise strike just inside the upright.
It was then Pedro's turn to get it right the second time.
The forward was denied by the post in the 14th, but he made it 2-0 minutes later when he volleyed home Dani Alves' cross after Xavi opened wide for the Brazil right back, who immediately picked out the forward charging free inside the area.
Messi almost had another goal in the 39th when he took Xavi's pass and chipped it over the onrushing Bravo, only to see the ball bounce off the woodwork.
Two minutes later Sociedad came to life when Castro was left unmarked on the left side to receive Carlos Vela's pass and rifle the ball past goalkeeper Victor Valdes.
Sociedad continued to apply pressure after halftime and soon found itself playing with an extra man when Pique was shown his second yellow card for cutting down Vela in the 56th.
"Going down to 10 men changed the game," Vilanova said. "I told Gerard that he would be sent off with the next foul."
The Basque side pushed short-handed Barcelona into its area and the effort paid off when Castro's strike took a deflection off substitute Javier Mascherano to catch Valdes out of position.
Barcelona was never able to serious threaten again, and the slow-footed Mascherano let Agirretxe slip behind him to reach Martinez's cross and end Barcelona's 15-game unbeaten streak in all competitions.
Ninth-place Sociedad stayed undefeated in six consecutive home games.
"There is no recipe for beating Barcelona," said Sociedad coach Philippe Montanier. "We won because we have a compact team that works hard, and we had a little bit of luck."
The defeat was Barcelona's first in league play since it lost to Madrid on April 21 of last season.
Earlier, Granada beat Rayo Vallecano 2-0 for its first home win in seven league games and moved three points above the relegation zone. Rayo, which remained in sixth place, saw its four-game winning streak come to an end.
Later, Sevilla visited Getafe, while Malaga hosted Celta Vigo.
On Sunday, Osasuna hosts Deportivo La Coruna, and Valladolid plays Real Zaragoza. Real Betis plays Athletic Bilbao on Monday.
Espanyol beat Mallorca 3-2 on Friday.