Barcelona headed into their game with Sociedad full of confidence after a Champions League win at Manchester City, but they were beaten in the corresponding fixture last term and endured another miserable trip to the Anoeta.
The hosts went ahead in the 32nd minute when Gorka Elustondo's header found the net via the shoulder of visiting defender Alex Song.
Lionel Messi equalised four minutes later with a typically clinical finish, but further home goals in the first 14 minutes of the second half - scored by Antoine Griezmann and David Zurutuza - ensured the Primera Division champions left San Sebastian empty-handed for the third time in four seasons.
Earlier in the day, Gareth Bale scored an outstanding long-range goal as Real Madrid beat Elche at the Bernabeu.
The world's most expensive player added to Asier Illaramendi's first-half opener with a stunning strike on 71 minutes, and then substitute Isco hit a third with 10 minutes remaining.
Although Madrid never hit top gear, they pulled off their 10th home win of the season to keep the pressure on title rivals Barca and Atletico Madrid, who play on Sunday.
Getafe remain in trouble near the bottom of the Primera Division after drawing 1-1 at Celta Vigo, but did well to pick up a point after playing the majority of the second half with 10 men.
Angel Lafita gave Luis Garcia's side the lead after 20 minutes but Getafe failed to cling onto their advantage and made things easier for Celta when Lisandro Lopez was shown a second yellow card early in the second half, shortly after which Rafinha equalised.
The day's late game pitted together two sides just above the drop zone in Almeria and Malaga, and there was nothing to choose between them in a goalless draw at the Juegos Mediterraneos.