The Nets learned in heartbreaking fashion why Dirk Nowitzki’s name is where it is on the NBA’s all-time scoring list.

Nowitzki scored the go-ahead bucket with 19.2 seconds left in overtime to propel the Mavericks to a 119-118 win over the Nets Wednesday night at Barclays Center. Nowitzki spoiled a wild Nets comeback and finished with 22 points, passing Shaquille O’Neal for sixth place on the NBA’s all-time scoring list with 28,609 points.

Once again, Deron Williams wasn’t in the lineup in Brooklyn, and it caused problems for the Nets. J.J. Barea — starting in place of the injured Williams, who missed his Brooklyn homecoming with a strained hamstring — had 32 points, 11 assists and converted the go-ahead three-point play with 12.4 seconds remaining in regulation. But Thaddeus Young, who entered the game having hit just 1 of 12 three-point attempts this season, drained a contested three with 6.3 seconds remaining to even the score at 108. On the ensuing possession, Young blocked Barea’s fadeaway shot as time expired to force overtime.

Both teams exchanged three after three as the lead changed hands multiple times in the extra frame, with Barea hitting a three for a 117-116 lead. Young later stripped ball from Barea, leading to Brook Lopez’s hook shot with 41.7 seconds left for a one-point Nets lead. Nowitzki, with Young defending in the post, backed in for the layup to give the Mavericks a 119-118 lead.

With a chance to win it, Jarrett Jack missed a jumper in the final seconds.

The Mavericks shot 70.6 percent from the field in the first half and led 64-53 at the break. But the Nets looked like a different team in the third quarter, scoring a single quarter season-high 34 points while holding Dallas to 17 points, to take a six-point lead heading into the fourth.

Young finished with 29 points and 10 rebounds and Bojan Bogdanovic added 17, hitting all five of his three-point attempts.