On O’Sullivan’s 44th birthday, the five-time world champion fought from 3-0 and 4-1 down to level at 4-4.
However, Ding composed himself to win the next two frames and set up a last-eight meeting with Chinese compatriot Liang Wenbo. Defending champion O'Sullivan becomes the fifth top-eight player to exit the tournament.
“I played really well at the start. He was missing long shots and gave me easy ones into the pack," Ding told Eurosport.
“I did well, he played quite good when I was 4-1 up and we started messing around a little bit. Normally he doesn’t miss easy shots.
“I love to see games like that. I’m enjoying this even with the pressure. I can’t remember [when I last played like that].”
Meanwhile, O'Sullivan said he was "happy" with his display despite falling at the fourth-round hurdle.
“I was quite happy to get four frames the way it was going at one stage. I hadn’t potted a ball for so long. I hadn’t done a lot wrong but to get back to 4-4 was great. I had a chance, didn’t take it, that’s just how it goes.
'No regrets' for O'Sullivan after defeat at UK Championship
"At 3-0 and 4-1, would I have taken a 6-4 loss with the chance to win? I probably would have done.
Ding is a great player, I’m surprised he hasn’t won as much as he has. I’m sure with his quality and pedigree and will to win that will fall into place.
“I was just please to be involved in a good match, I just wish he had played a worse match!”
Ding got off to a flying start when posting 278 points without reply to take a 3-0 lead.
'Beautiful snooker' - Ding on fire against O'Sullivan
Breaks of 82, 51 and 110 put Ding firmly in control before O’Sullivan finally potted his first ball in the fourth frame.
O’Sullivan’s 78 helped him reduce the gap to 3-1 as a swift encounter headed to the interval averaging 12 minutes per frame.
Once back out, Ding moved three frames in front before O’Sullivan hit back with a 77.
'What luck!' - Incredible fluke from O'Sullivan to middle pocket
Two rapid centuries of 107 and 124 saw O’Sullivan move level with Ding as the match headed for what was essentially a best-of-three showdown.
A crucial ninth frame went Ding’s way, but only after a miss with the rest was not punished by O’Sullivan, who had all reds open when missing one into the bottom-left corner.
Ding then needed two bites at the cherry, but took the 10th frame to seal a fine win over O'Sullivan.
O'Sullivan was not the only big name to have crashed out in York, with Mark Selby also suffering a surprise exit at the hands of world number 43 Matthew Stevens.
After racing into a 3-1 lead, Stevens was pegged back by Selby, who made it 4-3 but the Welshman found a way to stay in the contest to level it at 4-4 and 5-5 before claiming a tense marathon deciding frame.
Highlights from Day 6: Stevens stuns Selby in latest huge upset