The 32-year-old completed the hat trick when he tied it 5-5 with 2:12 remaining in the third period on a wrist shot from the top of the right face-off circle. It was his 850th NHL game.
Karlsson leads all defensemen this season with 15 points (nine goals, six assists) in 12 games. It was the first hat trick by a defenseman in Sharks history.
"The puck went in, which is all that matters," he said. "It's nice [for the team] to score five goals, but at the same time we let in five as well."
Adam Henrique scored his first two goals of the season, and Troy Terry and Kevin Shattenkirk each had two assists for the Ducks (3-6-1), who have won two in a row following a seven-game skid. Anthony Stolarz made 39 saves.
"It was an up-and-down game," Anaheim coach Dallas Eakins said. "There was a ton of emotion in the game, a ton of emotion on our bench. When you can bend and not break, and when you can learn to channel your emotions in the right direction, I think it's a great learning experience."