UEFA Champions League: Leverkusen 2 Sociedad 1
The midfielder, who had been brought on six minutes from time, curled home from distance as the Bundesliga outfit put their 4-2 opening-game defeat to Manchester United behind them.
Simon Rolfes had put the hosts ahead just before the break but Carlos Vela's rebound from his own saved penalty levelled the scores in the second half.
And the Primeira Liga side saw their hopes of a first point in Group A stolen away by Hegeler late on as they followed up their loss to Shakhtar Donetsk with another defeat.
There was just one change from the Leverkusen side that beat Hannover 2-0 last weekend, with Philipp Wollscheid replacing Emir Spahic at the back.
Midfielder Lars Bender was included despite limping off with an ankle injury on Leverkusen's last outing.
Sociedad manager Jagoba Arrasate made four changes from the side that drew 1-1 with Sevilla on Saturday, with Jose Angel Valdes, Gonzalo Castro, Ruben Pardo and Imanol Agirretxe all missing out.
Experienced midfield pairing Gorka Elustondo and David Zurutuza were both drafted in, as were defender Alberto De La Bella and striker Haris Seferovic.
The home side started brightly and the game's first opportunity fell to the in-form Stefan Kiessling, who has five goals in seven Bundesliga games this season, but he fired straight at Claudio Bravo after going through one-on-one.
Leverkusen almost went ahead in the 20th minute when Rolfes' strike deflected off Sidney Sam and forced a smart stop from Bravo.
Sociedad created some chances of their own, though, and 12 minutes before the break Elustondo's superb effort was well kept out by Bernd Leno.
But it was Sami Hyypia's men who took the advantage into the interval, Rolfes firing home after his header from Sam's free-kick had been parried by the goalkeeper on the stroke of half-time.
Their lead lasted less than 10 minutes into the second half, as Roberto Hilbert chopped down Vela and the Mexican stepped up to take the spot-kick, which he tucked away at the second attempt after Leno saved the initial effort.
But, after weathering the storm, it was the hosts who prevailed when Hegeler found the top corner with a stunning last-gasp free-kick that marked his side's 100th goal in the competition.