Curtis Brown scored the go-ahead goal 5:42 into the third period for the Sharks, who improved their preseason record to 5-1. Milan Michalek had a goal and two assists, while rookie defenseman Marc-Edouard Vlasic added his first goal for San Jose.
Patrick Marleau had three assists and Kyle McLaren added a pair.
Kristian Huselius had two power-play goals for the Flames (3-1-1). Jarome Iginla also scored and Jamie Lundmark had two assists.
Evgeni Nabokov made 30 saves to record his first win of the preseason for the Sharks, while Flames goalie Miikka Kiprusoff stopped 16 shots.
The Sharks opened the scoring 2:26 into the game on a power play when Vlasic's shot snuck past Kiprusoff, who was screened on the play.
The Flames responded during a man advantage of their own slightly more than two minutes later when Huselius swatted home a loose rebound past Nabokov.
Early in the second period, Alex Tanguay put a perfect pass onto Iginla's stick to send the Calgary captain on a breakaway. Iginla made a quick move and then slipped the puck through Nabokov's legs to put Calgary up 2-1.
Bernier scored goals 43 seconds apart to put San Jose up by a goal.
Huselius tied the contest at 15:58 of the second period when yet another power-play rebound bounced right onto his stick. All he had to do was direct it into the net behind Nabokov to even the score at 3-3.
In the third, Brown tipped in a point shot by McLaren before Bernier completed his hat trick when he fired a centering pass from Michalek past Kiprusoff. Michalek then added an empty-netter.