Brendan Gallagher and Andrew Shaw scored for the Canadiens (11-7-5), who have lost three in a row. Niemi made 37 saves.
"They're a team in our division and those are important points," Montreal coach Claude Julien said. "We had control of the game there until the last two and a half minutes when they tied the game up. I thought we played a good, sound game overall. We were much better defensively, but discipline at the end, we take a penalty ... and failing to clear the puck at times, and it ended up in the back of our net."
Casey Mittelstadt scored for Buffalo (15-6-2), and Carter Hutton made 31 saves.
"Whatever the score is, I think we keep to it," Hutton said. "Obviously, their power play comes through, they get a great goal. We stick with it there and we get a big goal late."
Mittelstadt made it 1-0 at 12:39 of the first period. Conor Sheary's deflection of Jake McCabe's shot-pass hit the right post and Mittelstadt tapped the puck into the open side of the net.