The win ensured that the tourists also retained their No. 1 test ranking.
Set 94 runs to win over two sessions, England started its innings poorly but reached the target in the middle session with the loss of two wickets at P. Sara Oval.
Sri Lanka opted to bowl spinners with the new ball and Tillakaratne Dilshan bowled out England captain Andrew Strauss in the first over with no runs on the board.
Rangana Herath trapped Jonathan Trott lbw with the total on 31. However, Alastair Cook and Pietersen steered their team to victory without further loss.
Pietersen continued his aggressive batting from his 151 in the first innings to score 41 runs from 28 balls to hasten the victory. His second spell included four boundaries and two sixes and he was awarded player of the match.
Cook hit six boundaries for his 49 runs that came off 69 balls.
Swann took six wickets to end Sri Lanka's second innings on 278. Sri Lanka made 275 in its first innings and England replied with 460.
Swann finished the second innings with figures of 6-106 for a match haul of 10-181; his best test bowling figures.
The victory ended England's disappointing run of four straight test defeats and extended its stay as the top- ranked test nation. England has struggled on Asian pitches that help spin bowling and lost three tests against Pakistan and the first test of this series in Galle.
A loss or even a draw would have promoted South Africa to top spot.
"We were all under a bit of pressure going into the game to make sure that we show that we learned some lessons over the four test matches," England captain Andrew Strauss said. "I am really delighted by the way we played.
"James Anderson was not rewarded for his bowling, I thought he bowled outstandingly well. Graeme Swann showed his real value to the side once again, creating problems all the way through. Kevin (Pietersen) in particular took the game away from the opposition in the way that only he can," said Strauss.
Playing down the importance of the No. 1 ranking the England captain added that after losing four straight matches he was more keen on winning this test.
"No. 1 is something that has never been in the forefront of my mind. If you do focus on that too much you take your eyes off the ball," he said. "It's nice to be heading to the English summer with a little bit of confidence. I am sure we are going to play well in England this summer."
Sri Lanka captain Mahela Jayawardene said that his team's poor start cost it the match.
"Pre-match I said that the first innings is very important especially playing in the subcontinent. We needed to take that advantage, get a good start and put the opposition under pressure. We didn't do that.
"We were playing a strong team, even when they were in trouble KP came and changed the game. We were outplayed in those first two days and that was the difference."
Resuming at 218-6, Sri Lanka batted until just before lunch, surviving some close chances as the batsmen struggled to handle sharp turn and sometimes steep bounce.
Overnight batsman Anjelo Mathews was dropped twice at forward short-leg by Cook but the fielder was successful in his third attempt, dismissing Jayawardene for 64.
Jayawardene faced 191 balls in his patient innings and hit four boundaries. His 90-run stand for the fifth wicket with Thilan Samaraweera promised to take Sri Lanka to safety but Swann's two wickets in three balls in the penultimate over of the fourth day turned the tide toward England.
Samaraweera made 47.
Mathews was the last batsman to fall when on 46 having stretched the lead to 93. The home side then briefly entertained a slim victory hope given England's poor reputation in handling spin bowling and its recent debacle in the United Arab Emirates when it chased 145 for victory in the fourth innings but was bowled out for 72 on a turning wicket.
From Our Network
- IPL 2016 final: RCB, Sunrisers to clash for maiden title win
- Kapil Sharma vs Bajirao Mastani: Who won the battle of TRP’s last Saturday?
- Sridevi's daughter Jhanvi to enter Bollywood soon?
- Subramanian Swamy accuses Raghuram Rajan of favouring US multinationals
- Mika Singh clears air on visiting ‘The Kapil Sharma Show’ despite Kapil-Krushna’s cold war