The 27-year-old France international had entered the final months of his previous deal and was linked with moves to Liverpool, Real Madrid and Paris Saint-Germain, but the extension ends uncertainty over his future.
Bayern board member for sport Max Eberl said the club were determined to retain a key figure in their squad.
"Upamecano is another central figure in our team who we've been able to extend with," Eberl said. "A squad needs anchor points, and we've got the next with Dayot. The question often isn't who you sign but who you develop. Dayot has matured into a top international player here at Bayern."
Upamecano joined Bayern from RB Leipzig in 2021 and has since made 180 appearances, helping the club win three Bundesliga titles.
"I'm really happy to be staying at Bayern and continuing to play in this team," Upamecano said. "We've got big goals together. I give my all for my teammates and want to win as many titles as possible with Bayern."