The fourth-seeded Serb was pushed hard by the world number 276, who had considered quitting the sport at the start of this year, before finally scraping through 6-7, 6-3, 7-6, 6-4.
"I think those matches have to appear sometime during the tournament," Djokovic said. "Maybe the good thing is that I have played this match in the third round, I have high ambitions for this tournament."
The 2007 Flushing Meadows runner-up said he had known nothing about Witten, who needed a wildcard to get into the qualifying draw, and almost paid a high price.
"He caught me by surprise and that's the worst thing you can have in the grand slams," the Serb said.
"It wasn't easy to play well against a guy that you don't know anything (about). I had never seen him play and then suddenly he comes up with those shots from the baseline and then returns. It was unbelievable."
Djokovic admitted he was guilty of letting Witten dictate too many of the rallies.
"I think I was too passive throughout but I was somehow finding the way to get through," he said.
"But it's hard when you have somebody so aggressive and really going for every shot and has nothing to lose and has no pressure. You have to find a way to stick with him all the time.
"So I'm just happy to get through, happy to get another win. I give him all credit for his performance today, absolutely.
"You can see that the rankings are something that can never really show the right picture because some of these guys can play top ten tennis."
Djokovic now plays Czech 15th seed Radek Stepanek for a place in the quarter-finals.
Below par Federer stops Hewitt in four sets
'I'm sure I will have sleepless nights'
Nadal thumps Djokovic, in final
'Another tough day in the office'
Stepanek upsets Roddick, in San Jose final