The deal includes a player option on the fifth year.
Middleton has developed into the co-star alongside MVP Giannis Antetokounmpo on the Bucks' championship-contending team, and his commitment Sunday reestablishes Milwaukee as an Eastern Conference favorite.
"We are not done. The goal wasn't to reach the Eastern Conference finals -- we are on a mission to win a championship," Middleton wrote in a post on ESPN.com. "I want to be a part of that mission, which is why I am staying here in Milwaukee for the next five years."
Middleton declined his $13 million player option to become an unrestricted free agent.
He was the Bucks' top priority in a free-agent class that includes Malcolm Brogdon and Brook Lopez. Lopez plans to re-sign in Milwaukee on a four-year, $52 million contract, sources said earlier Sunday.
Middleton, 27, emerged as an All-Star for Milwaukee, which had the best record in the NBA and reached the Eastern Conference finals. Middleton averaged 18.3 points, 6.0 rebounds and 4.3 assists last season. He's a career 39% 3-point shooter, a self-made player who emerged from being a 2012 second-round pick of the Detroit Pistons out of Texas A&M into one of the NBA's elite wing players.
NBA free-agent contracts can't be officially signed until Saturday.