The 47-year-old Welshman displayed some unlikely dance moves as he sought to avoid the persistent attention of the insect and wrap up victory.
In a bizarre moment during the match, referee Marcel Eckardt briefly suspended the eighth frame as he tried in vain to dispose of a wasp, and Williams joked afterwards: "I thought it was going to sting my nuts at one stage."
Williams, who won his first of two Masters titles in 1998, buzzed in back-to-back century breaks to start the match before a further run of 95 put him in a 3-0 lead.
The Welshman extended his advantage but Gilbert, handed his place in the top-16 tournament following the suspension of Yan Bingtao, rallied after the mid-session interval, winning two frames in a row to narrow the deficit to two frames.
Gilbert missed a couple of golden chances in the next, allowing Williams to reassert control at 5-2, before the wasp-hit eighth frame went Williams' way as he clawed his way back from a 63-point deficit.
"It's unbelievable how I'm still playing at the top of the game and I'm enjoying every minute of it," admitted Williams, who will face Ronnie O'Sullivan in the quarter-finals.
Earlier, the season's in-form star Mark Allen, who won the Masters title in 2018, suffered a shock 6-0 whitewash by Barry Hawkins.
Hawkins, who will face either Judd Trump or Ryan Day in the quarter-finals, told the BBC: "I think the result flatters me a little bit. I think my safety won me the match really. I played a lot of good safety and created chances that way.
"I think if I was ultra-critical, I could have scored a little better but I kept Mark under pressure and I didn't do that much wrong - very few easy unforced errors - and I think that's made the difference in the end."
Hawkins recovered from a 55-0 deficit in the opening frame to win it on the colours, with breaks of 76 and 114 then extending his advantage.
Allen looked set to get on the scoreboard in a scrappy fourth frame only to overcut the brown and leave it over the corner pocket, Hawkins clearing up to move further ahead before making it 5-0 with breaks of 45 and 41.
Having recovered from 6-1 down to beat Ding Junhui 10-7 in the final of the UK Championship in November, there was still a glimmer of hope for Allen, but the Northern Irishman was unable to reproduce such heroics and Hawkins sealed a commanding victory.
"For once I didn't have that much time off (over Christmas), I had a couple of days off here and there but kept going up the club because I knew this tournament was coming up and this is not a place you want to come here and be underprepared," Hawkins added.
"I thought I'd keep my head down and today it's paid off."
A disappointed Allen was quick to pay tribute to Hawkins, telling Eurosport: "I probably should have won the first frame and had a couple of chances in frame four, other than that I thought he was phenomenal.
"Some of his safety play was ridiculously good. There were a number of times I came to the table consecutively scratching my head, I had no shot at all.
"I said to him at the end, it's probably one of the best safety performances I've ever played against as a pro. Granted I missed a few balls as well but you just have to credit Barry, he just froze me out. It was a phenomenal performance."