Koivu's 11th goal this season after an assist by Markov proved to be the game-winner.
Jaroslav Halak was bombarded by 48 shots, but yielded two goals for the Canadiens, who have won four in a row after a three-game losing streak. They had lost four of their previous five meetings with the Sharks.
San Jose's goals, both in the second period, were scored by Joe Pavelski (power play) and Joe Thornton, whose 16th at 1:55 of the second drew San Jose within 3-2.
Brian Boucher was beaten three times on 21 shots for the Sharks, who had won five of six.
They lead the Western Conference with 93 points, but are just three ahead of second-seeded Detroit.
Pistorius testifies he didn't consciously pull trigger when he shot girlfriend
Pot vending machine to debut