Jayawardene, who is due to retire from Test cricket after the two-match series, scored 26 before Kumar Sangakkara (21) and skipper Angelo Mathews (25 not out) chipped in lower down the order.
Mathews and Kithuruwan Vithanage smashed 17 runs in the 16th over bowled by Junaid Khan and then scored the winning run in the next over just before rain hit Galle.
The win was set up by left-arm spinner Rangana Herath, who claimed six for 48 that bundled Pakistan out for 180 in their second innings in the post-tea session.
"It was one of the best games I have been part of," said Mathews.
"I was praying for a bit of sunshine and the rain staying away for some more time, but we managed to get through in the end."
Mathews attributed the win to Sangakkara's 221 in the first innings and Herath's six wickets earlier on Sunday, and hoped they would also win the final Test to give Jayawardene a perfect farewell.
"Sangakkara is such a great player and Herath was unbelievable," he said. "We want to focus on the next game and win it for Mahela, who is such an unbelievable player."- 'Dropped catches cost us badly' -
Pakistan captain Misbah-ul Haq rued the missed chances on the field, but admitted Sri Lanka were deserving winners.
"Dropped catches cost us badly, especially the one off Sangakkara," Misbah said, referring to the chance spilled at point by Abdur Rehman off the first ball of the fourth day when the batsman was on 102.
"Also our batting in the second innings could have been better. This was too good a wicket to get bowled out for 180. But the better team won. We must forget this game and get ready for the next one."
Pakistan, who started the day at 4-1, lost three wickets in the morning session to go to lunch on 66-4.
The tourists added eight runs in the morning when seamer Dhammika Prasad had nightwatchman Saeed Ajmal edging a catch to wicket-keeper Niroshan Dickwella.
Opener Ahmed Shehzad made 16 when he was given out leg-before to off-spinner Dilruwan Perera to make it 39-3.
Shehzad, who consulted his partner Azhar Ali before deciding not to ask for an umpire's review, would have survived if he had signalled for it. Television replays showed the ball missing the leg stump.
Herath dealt Pakistan a bigger blow when he bowled first innings century-maker Younis Khan for 13 to reduce the tourists to 55-4.
Ali and Misbah put on 56 for the fifth wicket on either side of lunch when both fell at the same total to make it 111-6 -- just 29 runs ahead with 45 overs left in the game.
Ali (41) was caught behind off Herath and Misbah (28) was leg-before to Perera in the next over.
Pakistan found themselves pinned against the wall at 133-7 when Asad Shafiq, the last recognised batsman, was leg-before to Herath for eight.
Seamer Shaminda Eranga had Abdur Rehman caught behind at the stroke of tea before Herath dismissed Mohammad Talha and last man Junaid to leave gutsy Sarfraz Ahmed stranded on 52 not out.
The second and final Test will be played at the Sinhalese Sports Club in Colombo from Thursday.