England's Selby, 33, who beat Ding 18-14 in May's Crucible final, took a 3-1 lead before his Chinese opponent hit back, knocking in a 75 break for a 6-3 advantage going into the interval.
Back came Selby with a 76 and century before he levelled the match at 6-6.
But Ding, 29, took control once more, recording breaks of 97 and 63 en route to completing his win on home soil.
All results from the Shanghai Masters here.
Ding Junhui won the Shanghai Masters for the second time with a 10-6 victory over world champion Mark Selby.