Dhananjaya de Silva and Kusal Mendis added an unbeaten 295 for the second wicket. De Silva posted the first 150-plus score of his Test career while Mendis played his ideal help from the other end.
Earlier, Bangladesh ended their first innings on 513 with Mominul Haque top-scoring with 176 and Mushfiqur Rahim and Mahmudullah Riyad adding 92 and 83 not out respectively.
Silva's hundred came off 197-balls, with six boundaries and a six, before he was dismissed trying to cut off-spinner Mehidy Hasan (3-173) in the 156th over to end a 115-run partnership with Chandimal.
Then, when the visitors finally called a halt to the run-fest, they ripped out three top-order wickets to leave Bangladesh with a hard task to save the game on the final day. "If our batsmen can survive the fifth day, then we can get something out of it. Batsmen would like to score runs off the bad balls".
De Silva, who was unbeaten on 104 overnight, reached his 1,000 runs in Tests on 154, joining Roy Dias as the quickest Sri Lankan to achieve the feat in his 23rd innings.
Silva shared a 135-run stand with skipper Dinesh Chandimal for the fourth wicket and hit six fours and a six.
Kayes will look back with disappointment at the stroke which brought about his downfall, attempting a ramp shot off the off-spinner Perera but only succeeding in toe-ending a catch to Chandimal at square leg. The off-spinner was at fault a few overs later when he could not get his hand on to an edge by Dickwella off Taijul Islam and the ball raced for four.
Sri Lanka wicketkeeper Niroshan Dickwella later said that despite the batting-friendly Chittagong pitch, it is their game to win.