Left-arm spinner Herath, who claimed nine wickets in the first innings, grabbed four for 46 to destroy Pakistan's fragile batting once again in front of some 5,000 screaming home fans.
Herath, who came on to bowl the ninth over after seamer Dhammika Prasad had dismissed openers Khurram Manzoor and Ahmed Shehzad, struck his first blow with his eighth delivery.
Jayawardene, who ends his 17-year Test career after this match, held two smart catches in the slips off Herath to get rid of Azhar Ali and skipper Misbah-ul Haq.
Herath then won a leg-before decision against Younis Khan to make it 50-5 in the 19th over, before Asad Shafiq and Sarfraz Ahmed stemmed the rot by batting an hour for their 55-run stand.
Shafiq was stumped off Herath for 32 and Abdur Rehman was leg-before to Dilruwan Perera, but Sarfraz remained unbeaten on a defiant 38.
Sri Lanka will return on Monday to take the remaining three wickets that will enable them to sweep the series after winning the first Test in Galle by seven wickets.
Pakistan, who need 144 runs more to record an improbable win, chose not to speak to the media after play.
- 'Magical Herath' -
Sri Lankan veteran Kumar Sangakkara said he was surprised that Pakistan lost seven wickets in one session after tea despite the difficult pitch that helped the bowlers.
"We did not expect Pakistan to collapse," he said. "But it's a challenge to bat on this wicket. It is two-paced, has inconsistent bounce and there is also a bit of turn.
"Dhammika gave us the early breaks and then the magical Herath was back. He has been incredible for us over the past few years."
The hosts were bowled out for 282 in their second knock just before tea, with seamer Wahab Riaz and off-spinner Saeed Ajmal claiming three wickets each.
Sri Lankan skipper Angelo Mathews remained unbeaten on 43 after Jayawardene was dismissed for 54 in his last Test innings.
The overnight pair of Jayawardene and his long-time team-mate Sangakkara, batting together for the last time in a Test match, took their partnership to 107 after Sri Lanka resumed the day on 177-2.
But both batsmen fell to Ajmal within three runs of each other as the hosts slipped to 189-4 within the first 30 minutes of play.
Ajmal had the left-handed Sangakkara caught at silly point by Ali for 59 and then removed Jayawardene in his next over after the batsman had added five runs to his overnight 49.
Jayawardene mistimed a lofted shot to mid-wicket, where Shehzad took a low tumbling catch.
One run earlier, Jayawardene had survived an appeal for a catch off Riaz as replays showed the ball touch the ground before landing in the wicket-keeper's gloves.The 37-year-old Jayawardene is one of only five batsmen to score more than 11,000 runs in both Test and one-day cricket -- the others being Sachin Tendulkar, Ricky Ponting, Jacques Kallis and team-mate Kumar Sangakkara.
The 149-Test veteran finished with 11,814 runs at an average of 49.84 with 34 hundreds and 50 half-centuries.
Having retired from Twenty20 internationals after Sri Lanka's title-winning campaign in the World T20 in April, Jayawardene will now play only one-day cricket heading into next year's World Cup Down Under.
Pakistan missed left-arm seamer Junaid Khan, who did not bowl in Sri Lanka's second innings after being hit on the helmet by a rising ball from Prasad on Saturday. He was kept under observation at a hospital for a few hours.
Team manager Moin Khan said Junaid may bat if needed.