Chittagong: Bangladesh, replying to Sri Lanka's first innings total of 587, were 409-8 at stumps on the third day of the second and final Test in Chittagong on Thursday.
It was a remarkable comeback by the bottom-placed Test side, who had been thrashed by an innings and 248 runs in the first Test in Dhaka last week -- suffering their 14th defeat in 15 matches against Sri Lanka with one draw.
But the loss of four batsmen in the final session after being 319/4 at one stage, meant Bangladesh still trail by 178 runs with just two wickets in hand.
Rahman and Kayes, who came together when Tamim Iqbal was dismissed without a run on the board on Wednesday afternoon, put on Bangladesh's highest second-wicket stand of 232.
Rahman scored 106 in only his second Test and left-handed Kayes celebrated his return to the Test side after two years with 115 to lift Bangladesh to 232/1 soon after lunch.
The duo surpassed their side's previous best partnership for the second wicket of 200 by Tamim Iqbal and Junaid Siddiqui against India in Dhaka in 2010, but they gave away their wickets with rash strokes.
Unorthodox spinner Ajantha Mendis bowled both batsmen and Kayes had to be taken off the field on a stretcher after pulling a hamstring as he stepped out to loft the ball.
Sri Lanka sliced through the middle order before Shakib Al Hasan retrieved the situation with a half-century and Nasir Hossain chipped in with 42.
Mendis dismissed Nasir and Sohag Gazi off successive deliveries, but Al-Amin Hossain prevented a hat-trick by defending the next ball.
At the close, Mohammad Mahmudullah was unbeaten on 30 and Al-Amin was on three.
Seamer Nuwan Pradeep conceded 13 no-balls in figures of 0-96 from just 15 overs. Mendis took 4-84 and off-spinner Dilruwan Perera claimed 3-119.
Bangladesh, perhaps inspired by Kumar Sangakkara's triple-century on the second day, dug in on the sluggish pitch where the ball tended to keep low at times.
Rahman and Kayes batted through the morning session to take the overnight score of 86/1 to 201 without further loss by lunch.
Rahman reached his hundred soon after lunch by driving Sri Lankan captain Angelo Mathews through the covers for a boundary.
Kayes was lucky to reach the three-figure mark after surviving two chances in one over from Mendis when on 95.
The first time he was dropped by Kithuruwan Vithanage at point, but when the same fielder took a catch three balls later, the umpire declared a no-ball.
Brief scores
Sri Lanka 587 (Sangakkara 319, Jayawardene 72, Shakib Al Hasan 5-148)
Bangladesh 409 for 8 (Kayes 115, Rahman 106, Shakib 50, Mendis 4-84)
Status Bangladesh trail by 178-runs