Jason Puncheon's strike seven minutes from the final whistle sealed the victory for Southampton in a match in which they had taken the lead three times previously.
Adam Lallana, Morgan Schneiderlin and Jos Hooiveld also found the back of the net for Southampton, but there was a setback when Luke Shaw left the field with what appeared to be a worrying knee injury.
The teenage left-back looks set to be ruled out of England under-21 duty this week and could be a doubt for Southampton's Premier League opener against West Brom next Saturday.
The Saints will also be concerned about the fitness of Nathaniel Clyne, who missed the game due to injury.
Otherwise there were plenty of positives for the Saints who took the lead after just seven minutes.
Fresh from his England call-up, Rickie Lambert found a great ball to put Lallana through and the midfielder produced a composed finish.
Southampton pressed forward with some good promising play but Sociedad equalised when Gorka Elustondo took advantage of some poor defending.
Saints soon retook the lead when Victor Wanyama put through Schneiderlin, who finished calmly.
Former Arsenal forward Carlos Vela pegged back Southampton again five minutes later as he took two touches to trap a diagonal ball and fire home.
The hosts went back ahead soon after with Hooiveld heading home Lallana's corner to make it 3-2 before half time.
The second half failed to match the fireworks of the first but Haris Seferovic's strike levelled the scores for Sociedad in the 59 minute before Puncheon's decisive late strike.