The Magic snapped a three-game losing streak and handed the Bucks their third straight setback.
Afflalo scored 36 points, the most he has ever produced in 435 NBA games played over seven years with three teams. He also had eight rebounds and six assists.
Nokola Vucevic broke free for a dunk at the end of the 24-second shot cluck that gave Orlando a three-point lead with 9 seconds remaining. Afflalo then stole the ball from O.J. Mayo to wrap up the victory.
Orlando fell behind by 19 points in the second quarter, but battled back to tie it late in the third.
Vucevic added 17 points and 11 rebounds. Maurice Harkless had 10 points and nine rebounds for Orlando. Mayo led the Bucks with 25 points.