Porte (BMC) attacked the leading group, which included all of the key players, with less than two kilometres to go and stayed in front to the finish at the top of the climb.
He finished ahead of Sergio Henao (Sky) and Michael Woods (Cannondale) to claim the 151km fifth stage from McLaren Vale by six seconds.
The effort not only delivered the stage for Porte but also pulled him up to second place overall, just nine seconds behind Gerrans (Orica-GreenEDGE).
“To be honest I never expected that one," Porte said. “Today the BMC guys were absolutely incredible and they backed me here, which is nice and I have only just started in this team.
“It’s always nice to win in Australia and on top of Willunga for the third time in a row, I am really happy. The crowds are just incredible, the Australian public just get behind this race so much. It really does feel like riding the Tour de France up this climb, incredible feeling.
Also making a big general classification move was mountain leader Henao, who clung desperately to Porte's wheel throughout the climb, an effort which saw him leapfrog Tinkoff's Jay McCartthy into third place overall.
The break of the day contained Pim Ligthart (Lotto-Soudal), Lars Boom (Astana), Nelson Oliverira (Movistar) and Reinardt Janse Van Rensburg of Dimension Data.
The trio spent three hours in the stage lead but were swept up inside the final five kilometres as Porte, Henao, Woods, race leader Gerrans and the other overall contenders prepared for the final ascent of Willunga Hill.
Gerrans now looks set to claim his fourth Santos Tour Down Under title with only the final 90km city circuit stage to go.
“One stage to go and anything can happen but I tell you what, I’d prefer to go into that stage in front rather than behind,” Gerrans said.
“This (Willunga) is the toughest stage of the race and where the GC is generally decided each year so if I’ve come out in front, hopefully that’s enough.
“I thought if I just kept my cool and ride my own pace, even when they attacked me in the final, it should be enough to hang on and I was just right.
“You never discount Richie Porte, especially on a finish like that, that he obviously loves. That’s three from the last three for Richie here.
"But my team were again there for me all day today and they delivered me in the perfect position and as fresh as possible into the final climb."