Roberto Soldado gave the visitors the lead midway through the first half but Inigo Martinez's volleyed goal put Philippe Montanier's side back on level terms.
Goals from Chory Castro and Imanol Agirretxe late in the second half appeared to have sealed the win for the Basques. However, Jonas pulled one back for Valencia just as injury time approached to set up a tense finish, before Agirretxe scored again to put the result beyond doubt.
The first half was an evenly matched but frantic affair. Carlos Vela very nearly gave the hosts the lead after two minutes, while Jeremy Mathieu came close to putting Valencia in front just a few minutes later, but his free-kick was tipped on to the post by Real Sociedad goalkeeper Claudio Bravo.
The opening goal came in the 25th minute when Aly Cissohko beat two Real Sociedad defenders on the left hand side to reach the byline before putting in a cross to Soldado at the near post, and the striker headed into the opposite corner.
Two minutes later La Real thought they should have had a penalty when Carlos Vela went to ground in the area following a challenge by Mathieu, but then Bravo kept out a Soldado lob at the other end but used his hands outside his area.
If referee David Fernandez Borbalan had seen the incident it would have been a certain red card for the Chilean, but, to the goalkeeper's fortune, he did not.
The decision would have a key impact on the development of the game, because the hosts equalised shortly afterwards. Castro whipped the ball into the box after taking a short corner and Inigo Martinez volleyed home on the turn at the far post to score a brilliant equaliser.
The second half took longer to get going than the first but just as the game looked as though it was heading for a stalemate, La Real launched a devastating counter-attack to take the lead and kick off a pulsating final 17 minutes.
Valencia failed to make use of a free-kick in a dangerous position and the ball was cleared towards Agirretxe, who played a quick one-two with Castro on the halfway line, before racing down the right wing and returning the ball to the Uruguayan striker to slot past Alves from six yards out. The goal came against the run of play but was a masterpiece of counter-attacking football
Agirretxe looked to have wrapped the points up with three minutes remaining, slotting in at the far post after a cross from Xabi Prieto, following another quick break.
However, a few moments later Jonas ensured there would be even more drama by heading the ball into the net to make it 3-2 after Bravo could only parry a shot from Pablo Piatti.
Los Che took desperate measures in search of an equaliser and were made to pay for it.
Agirretxe was the only Real Sociedad player in the opposing half and came back on to the halfway line to intercept the ball, before racing towards goal unchallenged.
With Diego Alves approaching, the striker lifted the ball over the keeper's head to send the home crowd and dugout into delirium.
The Basques are now five points clear of Ernesto Valverde's side in fourth place in the Primera Division table, with five games left.