The hosts, trailing by 12 runs in the first innings, were 86-2 in their second knock at tea on the third day at the Sinhalese Sports Club.
Kumar Sangakkara was unbeaten on 17 and the retiring Mahela Jayawardene, who was warmly applauded to the crease by Pakistani fielders and some 4,000 home fans, was on one in his last Test innings.
Tharanga made 45 in an opening stand of 54 with Kaushal Silva before both batsmen were dismissed by left-arm spinner Abdur Rehman in the post-lunch session.
Junaid, who took five wickets in the first innings, had been hit on the helmet by a rising ball from Dhammika Prasad in the morning session.
Earlier, Pakistan's wicket-keeper Sarfraz Ahmed smashed a maiden Test century and Sri Lanka's spinner Rangana Herath became the first left-arm bowler in history to take nine wickets in an innings.
Sarfraz hit a gutsy 103 as Pakistan carried their overnight score of 244-6 to 332 before they were all out at the stroke of lunch in reply to Sri Lanka's 320.
But Herath grabbed attention in the eventful morning session by claiming the four remaining wickets to finish with 9-127, only the second Sri Lankan after Muttiah Muralitharan to take nine wickets in an innings.
