The Sharks (40-19-8), who haven't lost to the Canadiens at home since Nov. 23, 1999 (3-2 in overtime), trail the Calgary Flames in the division and lead the third-place Vegas Golden Knights by nine points.
Tomas Hertl scored his 30th goal, and Martin Jones made 37 saves for San Jose, which has won three games in a row. Marcus Sorensen had a goal and an assist, and Joe Thornton, Timo Meier and Gustav Nyquist scored.
"I think we gutted out a win tonight," Sharks coach Peter DeBoer said. "I think we're dealing with a little bit of flu going through the team and a little shorthanded. I thought our goalie was our best player tonight, which gave us a chance, and I thought our third period, when we had to have a big period, we got our best period of the game. We did enough to win an important game."
WATCH: [All Canadiens vs. Sharks highlights]
The Canadiens (36-25-7) hold the second wild card into the Stanley Cup Playoffs from the Eastern Conference, two points ahead of the Columbus Blue Jackets. They are tied in points with the Carolina Hurricanes, who hold the first wild card.
Artturi Lehkonen and Andrew Shaw scored for Montreal, which is 3-2-0 in its past five games. Antti Niemi, who made 20 saves against his former team, started in place of Carey Price in the first of back-to-back games for the Canadiens, who play at the Anaheim Ducks on Friday.
"This place isn't an easy place for us to win, for sure," Lehkonen said. "We had our chances to get ourselves back in the game after trailing by two goals, but we couldn't. We have to ignore this game and focus on the one tomorrow night."
Hertl put San Jose ahead 1-0 at 6:55 of the first period when he scored an unassisted goal on a wraparound. It is the first time he has reached 30 goals in his six NHL seasons.
"I'm very excited about it because I still look at myself more as a passer," Hertl said. "I think I'm more a passer than a shooter, but lately the goals are going in and I'm happy."