10-man Barca suffers first league loss
All looked to be going to plan for Tito Vilanova's visitors when Lionel Messi and Pedro Rodriguez put them two goals up within 25 minutes.
But Gonzalo Castro's brilliant strike gave Sociedad hope before half-time and then, after Barca had lost center back Gerard Pique to a red card, Castro's curling effort was deflected into his own net by Barca substitute Javier Mascherano.
That sent the home fans wild, but it got even better in stoppage time when Imanol Agirretxe poked Carlos Martinez's cross from the right past Victor Valdes.
There was no sign in the early stages of the drama that was to unfold as the Catalan giants, who had headed to San Sebastian having dropped only two points in La Liga all term, started brightly.
Messi passed up a straightforward opportunity after five minutes but swiftly made amends, latching onto Andres Iniesta's pass and cutting in to slot a left-footed shot low past Sociedad goalkeeper Claudio Bravo from the edge of the penalty area.
It was the prolific Argentina forward's first goal in four attempts at the Anoeta, and meant he equaled a club record in scoring in 10 straight league games.
Pedro saw a brilliant curling effort hit the post after 14 minutes before being rewarded for his industry with a neat second goal. After a spell of solid possession, Xavi dinked the ball over the top for Dani Alves, who crossed for the Spain forward to prod home from close range.
Martinez thought he had pulled a goal back for the hosts in the 34th minute but was denied by the offside flag.
Messi almost made it 3-0 six minutes before the interval but again the woodwork came to the Basque club's rescue. Xavi was again the provider with the ball over the top and, with the out-rushing Bravo committed, Messi got there first and lofted a left-footed chip goalwards only to see it strike the right post and trickle agonizingly along the line before being cleared.
Moments later Castro halved the deficit for Sociedad. He was afforded too much space by Dani Alves on the left and, after being picked out by Carlos Vela, drilled a powerful low shot across Valdes.
Barca - 11 points clear of second-placed Atletico Madrid - immediately pushed forward once more looking to restore their two-goal cushion, their best attempt before half-time resulting in Messi's stinging volley being parried by Bravo for a corner in the 44th minute.
Ninth-placed Sociedad came out firing at the start of the second half, though, and had their visitors very much on the back foot.
Pique, having been needlessly booked before the break for not retreating 10 yards at a set-play, paid the price 11 minutes after the restart when he was shown a second yellow card for a trip on Vela.
Iniesta soon followed Pique into the referee's notebook when he clipped Martinez as he made another surging run towards goal.
The 10 men were looking rattled, Jordi Alba getting in the way of Antoine Griezmann's shot and then Valdes - who earlier this week announced he would not be renewing his contract when it expires in June 2014 - nervously spilling Castro's blast for a corner on the hour.
Soon after, the hosts were level. Mascherano, who had replaced Cesc Fabregas following Pique's dismissal, deflected Castro's right-footed effort and, although Valdes got a hand to it, he was unable to keep it out.
Barca's frustrations threatened to boil over 10 minutes from the end as Alba squared up to Sociedad substitute Diego Ifran.
Iniesta was then fortunate not to become the second visiting player sent off when he brought down Vela, the referee showing leniency following his earlier caution.
However, it did get worse for the leaders as the game entered stoppage time when Agirretxe tapped the winner past a helpless Valdes.