The Nets overcame an eight-point deficit at the start of the fourth quarter to win their fourth in a row and move within a game of New York for the lead in the Atlantic Division.
Williams made 1-of-2 free throws to give Brooklyn a three-point lead with 4 seconds to play.
After a time out, Milwaukee got the ball to Monta Ellis for a 3-point attempt that missed. Williams, however, was called for a foul that put Ellis on the line for three free throws.
When Williams missed the first one, he intentionally missed the next two in hopes the Bucks could grab the offensive rebound. But it was Brooklyn's Gerald Wallace who came up with the rebound to clinch the victory.
C.J. Watson scored 17 points for the Nets and Andray Blatche added 12 points and 12 rebounds.
Brooklyn overcame a 31-point effort from Milwaukee's Brandon Jennings, who also had 11 assists.