Pakistan 215 and 230 for 2 (Masood 114*, Younis 101*) need 147 more runs to beat Sri Lanka 278 and 313 (Mathews 122, Chandimal 67, Imran 5-58)
Scorecard and ball-by-ball details

Shan Masood and Younis Khan came together at 13 for 2 in a chase of 377, and responded with the highest fourth-innings partnership ever for Pakistan, leaving the visitors 147 runs adrift of achieving the highest successful chase ever in Sri Lanka.

The duo had to contend with a charged up three-man pace attack running in with a new ball. Masood had to overcome considerable nerves in general in his first Test outside the UAE, and discomfort against the short ball in particular. Their ally was the pitch, having slowed down enough for the ball to lose nearly all its threat along with its shine. Their pressure release was the inexperienced offspinner Tharindu Kaushal, who went for 92 in 20 wicketless overs.

The first ball of Pakistan's innings had summed up the task that lay ahead, when Dhammika Prasad squared up Masood with a ripper. Suranga Lakmal bettered that testing first over, beating Ahmed Shehzad with one that moved away and then uprooting his off stump with one that came in. Misfortune added to Pakistan's worries when Azhar Ali nicked behind down the leg side off Prasad in trying to whip a wide one.

The scoreline of 13 for 2 would not have helped Masood's jittery beginning, but the arrival of Younis did. Younis was eager to move across and solidly got behind the line of deliveries. He was also keen to look for the single. Slowly, Masood started to find some confidence. He was still not comfortable against the shorter lengths but moved forward to drill a few drives down the ground when the seamers overpitched.

The introduction of Kaushal in the 14th over changed the situation. The offspinner could not control his lengths, and repeatedly served up short balls and full tosses. Masood stepped out to lift him for fours, Younis pulled powerfully. By tea, Kaushal had conceded 43 from seven overs and the partnership had grown to 86 in 22.

Younis had been in no trouble till tea; he became almost imperious after the break. He started jumping across and thumping the fast bowlers on the up through the covers. He whipped Prasad through midwicket and cover-drove him for successive fours to march past fifty.

Masood had settled down, and though he was still getting beaten on the cut, he was also working the ball into gaps.

With his fast bowlers flagging, and his offspinner leaking runs, Angelo Mathews tried himself, and almost broke through. Sri Lanka reviewed for leg-before against Masood, on 79, but with replays inconclusive about whether there was an inside edge, the third umpire had to go with the on-field call.

Sri Lanka's spread-out fields helped Masood and Younis to keep rotating the strike. Masood reached his maiden Test hundred stepping out and lifting Kaushal for a straight six. Younis never allowed the offspinner to settle, trying the reverse sweep now and then, and made it 30 Test centuries with a swept four in the last over of the day. He also became the first man to make five fourth-innings hundreds.

In the morning, Mathews' fifth Test century had stretched Sri Lanka's lead to 376, even as Imran Khan picked up his maiden five-for with the second new ball.The overnight pair of Mathews and Dinesh Chandimal had furthered their rebuilding act to strengthen Sri Lanka's position, adding 50 runs more to extend their sixth-wicket partnership to 117.

The lead was nearing 350 when Imran finally got reward for his accuracy in the 16th over of the day, Chandimal going leg-before for 67. Prasad bagged a king pair, feathering one to the keeper.

Imran, who had bowled lucklessly in the first innings, wrapped up proceedings to herald lunch, when Mathews chased a wide one on 122, and became the fourth successive man to be caught by Sarfraz Ahmed. By then, he had left Pakistan the task of mounting the second-highest successful chase in Asia to win the series. Masood and Younis' 217-run partnership seems to have put Pakistan firmly on that path.

Abhishek Purohit is a senior sub-editor at ESPNcricinfo