Left-handed opener Imamul Haq, on 46, and skipper Babar Azam, on 26, were at the crease with more than 90 minutes and 26 overs of play left in the final session on day four.
Spinner Prabath Jayasuriya sent back first-match hero Abdullah Shafique for 16 with debutant Dunith Wellalage taking a good running catch at mid-off.
Dhananjaya de Silva hit 109 before Sri Lanka declared their second innings on 360-8 in the second session.
Tourists Pakistan, who won the first match of the two-Test series, face an almost impossible task if they are to make it 2-0.
They need to bat out over three sessions for a draw.
The West Indies hold the record for the highest-ever successful Test chase, 418 against Australia in 2003 at Antigua.
Sri Lanka had declared their second innings on 360-8 in the second session after resuming on 176-5.
De Silva made 109 before being run out at the non-striker’s end and skipper Dimuth Karunaratne called his batsmen back to the pavilion.
De Silva put on a key stand of 126 with overnight partner Karunaratne, a left-handed batsman who battled back pain during his knock of 61.
Beginning the day on 27, De Silva reached his ninth Test hundred with a cracking boundary off Mohammad Nawaz and raised his bat to the applauding dressing room.
Karunaratne fell in the first session after he escaped two close calls, which were unsuccessfully reviewed by Pakistan, off Nauman Ali’s left-arm spin.
Nauman finally got Karunaratne, caught at short leg, where Shafique took a sharp reflex catch.
Karunaratne, who had back spasms and did not field in the Pakistan innings, got help from the physio during his two hours and 45 minutes at the crease as he went past 6,000 Test runs.