Williams' hopes of beating the seven-time Masters champion seemed distant as O'Sullivan surged into a 3-0 lead at Alexandra Palace.
But the Welshman - who clawed his way back into the contest with a tournament-best break of 143 - won three successive frames to lead 5-4 and then held his nerve with another century in the decider to seal a semi-final spot against Hossein Vafaei or Jack Lisowski.
"The first three frames I didn't have a shot," Williams told the BBC after ending a run of six successive defeats to O'Sullivan, a run stretching back to the 2014 International Championship in China.
"He tied me up in knots, he was making breaks galore.
"But I felt the crowd started to sway towards me at the end and most of them wanted me to win, which was unbelievable."
Asked if it was his most satisfying win in recent seasons, the 47-year-old three-time world champion said: "Yeah, you're playing the greatest player ever… and I don't think I've beaten him in the UK for 20 years.
"That's gone now. If I don't beat him for another 20 years I couldn't care."
Second seed O'Sullivan seized early control with a break of 115 after Williams had failed to find an escape route when being tight to the green.
The second frame was a completely different affair, full of tactical safety play and lasting 36 minutes.
But O'Sullivan held the upper hand throughout and a clearance of 44 doubled his advantage.
Williams was punished again when he missed a risky red with the rest and O'Sullivan compiled a frame-winning 79 break.
The contest appeared to turn before the interval after O'Sullivan, among the balls and ahead in the frame, chose to wait until disturbing a pack of reds and completely missed them.
Williams capitalised with a frame-winning 55 break and, with the help of a wonderful underarm shot, delivered a decisive 83 in the next.
O'Sullivan's safety play helped him into a 4-2 lead, but Williams then produced a stunning 143 clearance - his best break in Masters history.
It also eclipsed John Higgins' tournament best of 142 and put Williams in line to collect the £15,000 prize for the highest break.
Another fluent 90 levelled the scores and, when O'Sullivan missed a dangerous red into the middle after choosing not to play safe, Williams made a 59 clearance to lead for the first time.
O'Sullivan won a tense 10th frame, making 77 after a long safety exchange with reds scattered near the bottom cushion.
But Williams was not to be denied victory, making the most of another O'Sullivan error to build a match-winning 102.
O'Sullivan said: "I just didn't score, didn't make no breaks. At 3-0 he struggled a bit, but he played the better snooker and was more clinical.
"I was happy to get it to five-all. I had the chance in the last, but when you're not cueing well blacks like that are missable.
"There was far too many mistakes in there, basic mistakes, and that's how it goes sometimes."