Bangladesh, trailing by 389 runs in the first innings, were bowled out for 299 in the final session of the third day's play to give the hosts a decisive 2-0 lead in the three-match series.
But Bangladesh captain Mohammad Ashraful and teenager Mushfiqur Rahim frustrated the hosts by adding 191 for the sixth wicket, their country's best ever Test partnership.
The 22-year-old Ashraful, leading the side in his first series, remained unbeaten on 129, his fourth Test century and the third against Sri Lanka. He hit 18 boundaries.
Rahim, 18, who replaced long-serving wicket-keeper Khaled Mashud for this match because of his batting abilities, justified his selection with 80.
The young duo surpassed Bangladesh's previous best partnership of 187 by Habibul Bashar and Shahriar Nafees for the second wicket against Australia in Fatullah last year.
It was left to prolific off-spinner Muttiah Muralitharan, kept wicketless for 26 overs, to break the partnership after tea when he accepted a return catch from Rahim.
That triggered a dramatic collapse as Bangladesh lost their last five wickets in less than an hour, sliding from 269-5 to 299 all out.
Chaminda Vaas finished with four for 55, while Dilhara Fernando claimed 3-60. Muralitharan was restricted to just one wicket at the cost of 84 runs.
Sri Lanka had won last week's first Test at the Sinhalese Sports Club by an innings and 234 runs.
Sri Lanka have now won all their nine Test matches against Bangladesh since their first encounter in 2001, six of them by innings margins.
Five of the Tests ended in three days and the remaining four early on the fourth morning.
It was Bangladesh's 42nd defeat in 48 matches since they gained Test status in 2000 with five draws and a lone victory against Zimbabwe.
Ashraful said he was happy at scoring a century, but rued the team's collapse for their lowest Test score of 62 in the first innings.
"It is never easy to come back from that position," he said. "We just have to bat better in the first innings and get a big score.
"It was a great partnership with Mushfiqur and we showed that Bangladesh was up for the challenge.
"But we needed at least two other such big stands to save the game. The boys are trying hard to learn the art of playing Test cricket. We won't give up."
Sri Lankan captain Mahela Jayawardene attributed the win to an all-round effort after Kumar Sangakkara had made an unbeaten 200 on the second day.
"Sangakkara really shored up the innings although I was a bit disappointed that the other batsmen could not go on to get big scores," said Jayawardene.
"But I can't praise the bowlers enough for toiling hard on a wicket that had nothing for them.
"Vaas is a top performer as is Murali, they are great at creating opportunities. It helps that we also have two bowlers in (Lasith) Malinga and Fernando who bowl consistently at 90 miles an hour."
The final Test starts in Kandy on July 11.