Australia had already won the series by Shanaka (54 not out off 25 balls) and started hitting fours.
Australian skipper Aaron Finch and David Warner gave Mahesh Thikshana a 43-run lead in six overs before dismissing them for 29.
Glenn Maxwell (16), Warner (39) and Josh Inglis (0) were all out in quick succession, and it looked easy for Australia until a team’s hat-trick knocked them out.
Steve Smith (37 not out) and Marcus Stoinis (38) reversed the situation, adding 48, while Kusal Mendis tripped Stoinis as Australia set a target of 177.
The hosts were in a hopeful 67-1 position, but once Pathum Nisanka (27) and Charit Aslanka (26) were gone, the situation looked unclear.
However, Shanaka suddenly bounced back with each ball and shared a 50-run stand off 21 balls with Chamika Karunaratne (14 not out) and needed 19 runs to win.
Ken Richardson bowled two wide before two singles and three fours as Sri Lanka needed just one on the final ball, which came after Richardson bowled another wide.
The last captain’s game from Shanaka
Many were wondering if Shanakahi should be in the team going forward in this game. Most likely they are not investigating him anymore.
Shanaka hit five fours and four sixes as he almost single-handedly turned the game around, including 14 off three balls in the final over.
Hazelwood turns from hero to villain
Josh Hazlewood was particularly stingy in his bowling, finishing 2-3 after three overs.
However, his final over went to 22, Shanaka hit him with two fours and two sixes and the veteran bowler’s final tally increased to 2-25 in four overs.