Ward scored a shorthanded goal at 3:44. Niemi came out of his crease to play the puck, and Ward banged into him before putting the puck into an open net for his 21st goal of the season.
Hertl scored his 19th goal at 5:48 of the third, knocking a rebound past Niemi when the Sharks cut the lead to 3-2. Niemi made a pad save on Joe Pavelski's backhand from close range but couldn't control the puck.
"I thought we battled hard in third period," Sharks forward Tommy Wingels said. "Coming back from 3-0 and we're talking about making a push. We had just too many missed opportunities. We pressed and attacked and took pucks to net and we were physical."
San Jose went on its sixth power play of the game at 13:24 of the third, but Dallas remained perfect on the penalty kill. Pavelski had a shot from the slot, but Niemi made a glove save with more than six minutes left.
"Really exciting day," Niemi said. "Knew I'd get a game at some point this week on this road trip. Wasn't sure if it would be this game or not. Really exciting game, exciting day. It's been a while since the last game and also since the last win so it feels pretty good."