Media playback is not supported on this device
A rousing comeback saw Ding level at 5-5 from 5-1 down but a brave final frame from his opponent produced a surprise victory in a thrilling match.
Ronnie O'Sullivan eased past Ben Woollaston with a comfortable 6-2 victory to reach the last 16.
Defending champion Neil Robertson overcame Peter Ebdon 6-2.
The Australian was in ruthless form, compiling breaks of 108, 84, 74 and 78 to go through.
And Northern Ireland's Mark Allen went out after losing 6-4 to England's Rod Lawler.
The world number six from Antrim had raced into a 3-0 lead but Lawler, from Liverpool, recovered to win six of the next seven frames with a best break of 110.
World number three Ding, who won five ranking titles last season, was considered one of the favourites and won the event in 2005 and 2009.
He opened the match with a superb 134 break but was blown away by his opponent, who won five frames in a row.
After winning four successive frames himself, a Ding mistake allowed the 18-year-old from Blackpool to take control in the deciding frame and seal the biggest win of his career.
"When I missed the chance to win 6-1, he started coming back at me, and it's the worst feeling in the world," Cahill told World Snooker.
"In the last frame I expected him to win in one visit, but after he missed I played a solid frame. My family went through the emotions as much as I did."
Cahill is the nephew of Stephen Hendry and only turned professional last year.
World number four O'Sullivan, playing with a broken ankle, described the tables as "shocking" following his win in the previous round.
But he completed a routine victory over world number 37 Woollaston.
"The first two matches I was struggling, but my ankle feels fine now," O'Sullivan told BBC Sport.
"I have to say the tables are playing unbelievably better now and well done to World Snooker for sorting that out."
O'Sullivan struggled with his long potting to trail 2-1, but made his 766th career century as he booked a meeting with Matthew Selt.
Graeme Dott  recorded a 6-0 win over Robert Milkins, who was suffering from an eye infection, while Stephen Maguire beat Mark Williams 6-2.
Media playback is not supported on this device
China's Ding Junhui suffered a shock 6-5 defeat against world number 100 James Cahill in the third round of the UK Championship in York.