Set a challenging target of 377 runs, the tourists recovered from a shaky 13 for 2 to move to 230 without further loss by stumps on a rain-free fourth day.
Masood, the five-Test old left-hander, was unbeaten on 114, having reached his maiden century by lofting off-spinner Tharindu Kaushal for a six over long-on towards the end of the day’s play.
Younis, who made his 100th Test appearance in the second match of the series, got to his 30th century in the day's last over. The pair have put on 217 so far for the third wicket, with Younis on 101.
Pakistan will enter the final day's play on Tuesday needing a further 147 runs with eight wickets in hand on a wearing, but easy-paced pitch at the Pallekele International Stadium.
If Pakistan win, it will be the first time a visiting team will have scored more than 300 runs in the fourth innings to win a Test on Sri Lankan soil.
Pakistan's highest successful chase is 314 runs they made against Australia to win the Karachi Test way back in 1994.
Earlier, Sri Lanka's captain Angelo Mathews was last man out for 122 as the hosts took their overnight score of 228-5 to 313 before they were all out at the stroke of lunch.
Pakistan's fast bowler Imran Khan took all the five wickets that fell in the morning session in the space of 33 balls.
Mathews, who came in to bat on Sunday afternoon with his team reduced to 35-3, anchored the innings for more than six hours in which he hit 12 boundaries and a six.
Image: Younis Khan
Photograph: Ryan Pierse