Bangladesh, chasing a rain-revised target of 183 off 27 overs, won the day-night match with six balls to spare in Pallekele to register their fourth one-day victory against Sri Lanka in 33 matches.
The tourists needed 17 to win off the last 12 deliveries, but Nasir eased the pressure when he smashed three fours in an over from seamer Thisara Perera before Sohag Gazi hit the winning boundary.
"It's a huge win for us," said Bangladesh skipper Mushfiqur Rahim.
"It was a challenge for us to do well against big teams, so I am very happy with the effort. We have not been in such situations before and that's why the boys were panicking, but we pulled it off."
Bangladesh began their chase on an impressive note when Anamul Haque (40) and Mohammad Ashraful (29) put on 77 for the opening wicket in 12.5 overs before completing the upset win in the penultimate over.
Sri Lanka won the first one-dayer by eight wickets in Hambantota, while the second match at the same venue was abandoned due to rain.
Tillakaratne Dilshan earlier scored a 128-ball 125 to help Sri Lanka post a challenging 302-9 in 50 overs. The in-form opener, who made an unbeaten 113 in the first match, hit 12 fours in his 16th one-day hundred.
"We were disappointed as we had runs on the board. The bowling let us down today. We batted brilliantly even though we lost our way in the middle," said Sri Lanka captain Angelo Mathews.
Dilshan, who reached his century with a single off left-arm spinner Abdur Razzak, was the seventh man out, caught by Nasir at mid-wicket off paceman Shahadat Hossain in the 47th over.
Razzak, who removed Nuwan Kulasekara and Sachithra Senanayake off successive balls in his last over, finished with 5-62, his fourth five-wicket haul. He also completed 200 wickets in one-day internationals.
Sri Lanka were reduced to 263-7 before Lahiru Thirimanne hit a 15-ball 25 not out late in the innings to help his side cross the 300-mark.
The hosts got off to a solid start after electing to bat as Dilshan and Kusal Perera (56) added 116 for the opening wicket.
Dilshan then put on 87 for the next wicket with Kumar Sangakkara (48), who completed 11,000 one-day runs during his 49-ball knock.
Perera started impressively as he lofted Shahadat over long-on for the first six of the match and then hit the same bowler for three fours in an over.
Playing his seventh one-dayer, Perera fell after completing his maiden half-century as he was caught by Nasir at point while attempting to cut off-spinner Mohammad Mahmudullah.
Sri Lanka and Bangladesh will now play a one-off Twenty20 international in Pallekele on Sunday.