The Black Caps went into the final over needing seven runs and with three wickets remaining. Matt Henry was run out off the third ball but Williamson struck a four off the fourth to draw the scores level.
There was no score off the next ball but, although Williamson missed his pull attempt at the final ball, he hurled himself towards the non-striker's end and got his bat over the line just before the ball struck the stumps to secure a two-wicket victory.
Rain had washed out the morning as the Black Caps began the day on 28 for one, needing 285.
Williamson, who finished with 121 not out, survived a review shortly after play did get under way and was the rock in New Zealands innings, with Daryl Mitchell - who made 81 - the only batter to stay with him for any length of time.