Karunaratne (55) and Mendis (61) negotiated the dangerous spin duo of Ravichandran Ashwin and Ravindra Jadeja after opener Upul Tharanga fell cheaply for the second time in the game. The home team had played out 29 overs by tea.
Post lunch, Lanka began their second innings after being bowled out for 183 in reply to India's 622 for nine.
Virat Kohli again opened with spin from one end, handing Umesh Yadav (1-17) and Ashwin (0-51) the new ball.
But Karunaratne and Mendis stood firm to defy the Indian bowling. Key to their stand was seeing out Ashwin's initial spell as Mohammed Shami (0-8) was soon introduced into the attack along with Jadeja (0-42).
Also Read
The two batsmen left the ball well and played some brilliant strokes to keep the runs ticking along as Lanka's 50 came up in the 12th over.
India missed one chance when Shikhar Dhawan dropped Mendis at mid-on, when he was batting on 1, off Ashwin in the 8th over.
Mendis also survived a caught behind appeal off Jadeja via DRS as the ball touched the thigh pad and was never even close to the edge. Karunaratne scored his 13th Test half- century off 83 balls, while Mendis scored his 5th Test half- century off 53 balls as they countered the Indian bowling for once.
Earlier, in reply to India's mammoth total, Sri Lanka collapsed to 183 all out at lunch conceding their highest-ever first innings' lead in Test cricket - 439 runs, more than 419 runs against Pakistan at Galle in 2000.
Starting from overnight 50/2, it was surprising to see Ashwin not handed the ball straight away. Instead, Umesh Yadav (1-12) and Ravindra Jadeja (2-84) started proceedings, getting instant results.
Dinesh Chandimal (10) was the first to go, in the fifth over of the day, caught at square leg off Jadeja. In the very next over, Yadav had Mendis (24) caught at mid-on, striking a double body blow to Sri Lanka's hopes of defiance.
It brought Angelo Mathews (26) and Niroshan Dickwella (51) to the crease, and the duo decided to attack the Indian bowling, particularly the spinners.
They put on 53 runs for the 5th wicket taking Lanka past 100 in the 33rd over.
Their 50-partnership came off only 46 balls but this attacking ploy didn't stand for long. Cheteshwar Pujara caught Mathews at leg slip off Ashwin in the 34th over, a superb catch as he dove to his left, and broke the partnership. It also began Lanka's collapse as they lost their last 6 wickets for 66 runs in 15.4 overs.
Jadeja then bowled Dhananjay de Silva (0) first ball to pick up his 150th wicket in Test cricket. He became the quickest left-arm spinner to achieve this feat, in 32 Tests, and the sixth-quickest among all bowlers.
Meanwhile Dickwella continued to attack and reached his 5th Test half-century off 44 balls. He perished shortly afterwards, bowled off Shami (2-13) in the 42nd over, scooping the ball back onto his stumps. Five balls later, Shami also bowled Rangana Herath (2) with a perfect yorker.
Ashwin returned to clean up Dilruwan Perera (25) with an off-break delivery. Then, injured Nuwan Pradeep (ruled out of action with an injured hamstring for atleast two weeks) came out to bat, but faced only six balls as Ashwin bowled him too to end Lanka's innings in the 50th over.
On day one, KL Rahul (57) scored a sixth successive Test half-century on his return to international cricket.
India were reduced to 133/3 at one stage, but Pujara and Rahane rescued them by scoring contrasting hundreds.
On day two, R Ashwin (54), Wriddhiman Saha (67) and Ravindra Jadeja (70 not out) scored half-centuries to help India post another 600-plus total.
Disclaimer: No Business Standard Journalist was involved in creation of this content