The Brazilian substitute salvaged a point for Real Madrid by firing home from close range in the 89th minute after Portu had given the visitors a second-half lead.
But it was not enough for Zinedine Zidane's side to leapfrog Barcelona into second place and they are five points adrift of leaders and city rivals Atletico Madrid, who have a game in hand.
The hosts dominated the first half and twice hit the woodwork, but could not force that elusive first goal.
But Real Sociedad were transformed at the start of the second period and – after forcing three-successive corners – they took the lead through Portu.
Former Manchester City winger David Silva orchestrated the move and from Nacho Monreal's cross, Portu rose highest to send a superb header in off the woodwork in the 55th minute.
The visitors spurned a chance to extend their lead when Portu failed to pick out the unmarked Alexander Isak in the area.
Casemiro headed Vazquez's cross narrowly off target and Rodrygo fired wide as Real Madrid chased an equaliser.
The hosts threatened again through Casemiro following substitute Hugo Duro's cutback and with time running out they finally hauled themselves level.
Real Madrid poured forward in the closing stages, but Real Sociedad held firm to halt the home side's five-game winning run.