The 45-year-old was handed the reins last year following Vern Cotter's exit and has managed to build on the foundations laid by his predecessor, winning nine of his 14 Test matches at the helm.
Townsend guided the Scots to third place in the Six Nations earlier in the year, which included an emphatic Calcutta Cup win over England at Murrayfield.
The Scottish Rugby Union have now decided to reward the former Glasgow coach for his efforts with a new deal, much to the delight of Townsend as he continues working towards next year's World Cup.
"I have always been incredibly proud to coach Scotland and I'm delighted and grateful to be able to extend my involvement with the national team," said Townsend.
"It's a privilege to coach such a hard-working group of quality players, which is continuing to grow in depth."
Townsend's coaching staff have also received extensions, with assistant Matt Taylor penning a deal until 2021, while attack coach Mike Blair is now contracted until 2020.
Scottish Rugby chief executive Mark Dodson added: "I'm delighted that Gregor has decided to continue as Scotland head coach until 2021.
"I was convinced that Gregor was going to make an outstanding head coach for Scotland and we're delighted to have secured his services for the next three years, at a very exciting time for Scottish Rugby."