Sri Lanka 419 (Chandimal 155, Dickwella 83) and 138 (Dickwella 40*, Yasir 5-51) beat Pakistan 422 (Azhar 85, Herath 5-93) and 114 (Herath 6-43, Dilruwan 3-46) by 21 runs
How did we get here? How did we hurtle, from the dullest 11 sessions of Test cricket possible, to this stirring, heart-palpitating, dramedy of a conclusion? Sri Lanka, out of the match surely when they set out to defend 136 after lunch, yet victors by 21 runs, Rangana Herath breaking records, scuttling Pakistan again - 6 for 43 to his name. The pitch began to sing for him, the batsmen played him with fatal hesitation, Dilruwan Perera was even denied the winning wicket by a no-ball, just so Herath could complete 400 Test wickets - the first left-arm spinner to the milestone.
With this mild insanity of a spell, he has confirmed himself one of the greatest defenders of scores in Test history. He has 11 fourth-innings five-wicket hauls now. Muttiah Muralitharan and Shane Warne? Puh, only seven apiece.
When he floated one up beautifully to Mohammad Amir, and then got it to connive between bat and pad to hit middle stump, Herath also became the first bowler to complete 100 wickets against Pakistan. Perera thought he had Yasir Shah caught at short leg, prompting wild Sri Lankan celebrations, but was found to have overstepped. Herath doesn't over step. His was the final triumphant act - a slider to nail poor Mohammad Abbas in front of off stump.
Pakistan were visibly heartbroken. How had they lost this Test after Yasir's 5 for 51 had swung the match almost completely in their favour? How had they been such kings with the ball in the morning, taking 6 for 69, and then become such klutzes with the bat in the next two sessions, surrendering all ten wickets for 114? So go Sri Lanka v Pakistan Tests, though - surely the most unpredictable and close-run rivalry of the past 10 years. The last time these two teams played, Sri Lanka set Pakistan a mammoth 377 runs to win, and still managed to lose.
It was Herath himself who had set Pakistan's slide in motion, fooling Sami Aslam in the fourth over of the chase, getting the batsman to play for the turning ball, but sliding it into his outside edge - eventually gobbled up at slip. After Suranga Lakmal dismissed a leaden-footed Azhar Ali and Perera took two wickets himself, Herath produced perhaps the two biggest wickets of the innings. Asad Shafiq shaped to cut a Herath delivery bowled shorter, and a little quicker, but managed only to send it to slip. Sarfraz Ahmed seemed to be winning a little momentum for Pakistan as he and Haris Sohail put on a partnership of 42 runs, but could not get bat on ball when he ran at Herath, and the ball dribbled off his pads to Niroshan Dickwella, who completed the stumping with Sarfraz barely in the frame.
Following those dismissals, Sri Lanka appeared the likelier side to lock the match down. The Sri Lankan fifth-day tropes were evident in abundance: the over-appealing wicketkeeper, the non-stop yammering around the bat, the exuberant celebrations. Kusal Mendis even experienced a moment of instant karma, when he took what he thought was the winning catch, performed Hasan Ali's trademark celebration in the direction of the dressing rooms, only for the batsman to be recalled because it had been a no-ball.
In the end, the final celebrations were a little more muted, both on the field and among the support staff. Following the lavish home thrashings suffered at India's hands, Sri Lanka were desperate for a victory to win their public back. Herath was the perfect figure to lead the PR exercise, but Dinesh Chandimal, Niroshan Dickwella, Perera and Dimuth Karunaratne, had all played their roles as well.
Pakistan, meanwhile, came face-to-face with life post Misbah-ul-Haq and Younis Khan. They have collapsed this way even with those two in the team, of course, but perhaps not quite so meekly. Yasir had bowled with venom, turning his legbreak practically out of the next district, slipping in that slider that seemed no less destructive. Abbas took the first wicket of the day, seaming one away, then back into Mendis who was hit in front of off stump. The remainder of the morning belonged more or less to Yasir. He dismissed the opposition spinners, getting Perera lbw with a slider, before having Herath caught at short leg next ball. No Sri Lanka batsman failed to suffer nervous moments against Yasir. In fact, Dickwella, who got what is in retrospect an invaluable 40 not out, had considerable luck as he swept, reverse swept and drove Yasir for runs. Pakistan, understandably, had left for lunch with a strut.
The last-day surprise is now something of a trope of Sri Lanka-Pakistan matches, several compelling conclusions having been summoned by these teams since 2009. This game, perhaps has had the strangest final five sessions at all. At one stage, Sri Lanka were in control. Then Pakistan took the match by the collar. The first innings, after which only three runs separated the teams, now seems like a long set-up for what eventually became a gripping second-innings shootout.
Andrew Fidel Fernando is ESPNcricinfo's Sri Lanka correspondent. @andrewffernando