His blunt message, given in a statement, was in response to reports that US President Donald Trump is planning to accelerate the withdrawal of American forces from Afghanistan, where they support a NATO mission, and from Iraq.
"The price for leaving too soon or in an uncoordinated way could be very high," Stoltenberg said.
"Afghanistan risks becoming once again a platform for international terrorists to plan and organise attacks on our homelands. And ISIS (the Islamic State group) could rebuild in Afghanistan the terror caliphate it lost in Syria and Iraq," he said.
The warning was unprecedented criticism by Stoltenberg of a Trump decision. Up to now, the NATO secretary general has been careful to not publicly contest statements by Trump relating to the Alliance.
But after his defeat in US elections this month by Democrat Joe Biden, Trump - who remains in power until January 20 next year - fired his defense secretary Mark Esper, replacing him with Christopher Miller, who said last weekend of American troops in conflicts abroad: "It's time to come home."
Trump reportedly wants to cut US troop numbers in Afghanistan to around 2,500, from more than 5,000 currently, before his term ends. He also reportedly wants to pull out 500 soldiers from Iraq.
Stoltenberg in his statement repeated what he said in the past, that NATO troop levels in Afghanistan remained under continual review and that, "when the time is right, we should leave together in a coordinated and orderly way".
He said there were currently fewer than 12,000 NATO soldiers in Afghanistan, "and more than half of these are non-US forces". Funding for their mission to train and assist Afghan security forces was allocated "through 2024," he added.
The German defence ministry responded to the media reports by saying it expected any US troop reduction in Afghanistan to take place "in close coordination with all partners on the ground".
"We remain true to the principle of: we go in together, we leave together," a ministry spokesman said.
Agencies contributed to this report.