The Serb was beaten 6-2 6-1 by the world number 42, who only came into the tournament as a lucky loser.
Sonego will face Dan Evans in the semi-finals after the Briton outlasted Bulgarian Grigor Dimitrov.
Evans showed plenty of determination to come through 7-6 (7-3) 4-6 6-2 in two hours and 42 minutes.
It is the second week in a row that the world number 33 has reached the semi-finals, after Antwerp last week, where he lost to Ugo Humbert.
"It feels great," Evans said afterwards. "I'm happy to come through a difficult match."
Earlier, Djokovic looked out of sorts in the opening set of his match as his 25-year-old Italian opponent took control.
He kept up the intensity in the second set, a double break putting him 4-1 up, and held his nerve for a famous win.
Sonego lost to Aljaz Bedene in the final round of qualifying but earned a spot in the main tournament after the withdrawal of Diego Schwartzman.
He is yet to drop a set on his way to the semi-finals.
"It's the best victory of my life. Novak is the best in the world," he said.
"Today I played so, so good. It's unbelievable. It's amazing."
It is the first time Djokovic has ever been beaten by a lucky loser.
It also means he will have to wait a little longer before he completes the formality of equalling Pete Sampras' all-time record of ending the season top of the rankings for a sixth time.
The other semi-final will feature South African Kevin Anderson against fifth-seeded Russian Andrey Rublev.
Anderson beat Russian fourth seed Daniil Medvedev 6-4 7-6 (7-5) while Rubev defeated out-of-form second seed Dominic Thiem 7-6 (7-5) 6-2.