The announcement, which was made just before Leipzig's first home game of the season against Eintracht Frankfurt, ends speculation that Bayern would sign Werner on a free transfer in 2020.
"I am happy to have extended my contract. It was a long process, but I wanted to be completely sure that I could make the next steps in my development at this club," said Werner.
The 23-year-old striker had previously held firm against all new contract offers, and openly hinted at his interest in a move to Munich last season.
Yet Bayern's interest appeared to cool over the course of the summer, as the German champions instead signed Ivan Perisic and Philippe Coutinho on loan to bolster their attack.
According to Kicker magazine, a new buyout clause in Werner's contract means that Bayern would now have to pay around 30 million euros ($33.7 million) to bring the striker to Munich.
Leipzig sporting director Markus Kroesche said the new contract was a "positive signal" for the club, as they look to challenge for the Bundesliga title under new coach Julian Nagelsmann.
"It underlines our aim to keep key players with us in the long term and to establish ourselves at the top of the league," he said.