The Bulls ended a three-game losing streak by beating the team with the worst record in the Eastern Conference, but it took an unlikely shot to pull out the victory.
Dunleavy attempted and missed a 3-pointer with 1:23 to play and the Bulls trailing by five, but he was fouled on the shot.
He then made all three free throws to bring the Bulls within two, after which Carlos Boozer lifted Chicago into a tie on a jumper with 57 seconds left.
Khris Middleton pushed Milwaukee back in front by making a leaning jump shot with 30 seconds remaining, but Dunleavy came through with his long-range attempt that slammed off the backboard. Joakim Noah blocked Mayo's attempt at a winning jump shot with two seconds to go.
Noah scored 21 points and had 18 rebounds. All five Chicago starters reached double figures with Jimmy Butler scoring 16 points and Boozer adding 14.
Gary Neal had 17 points off the bench for the Bucks, who have five victories in 23 games this season.