WASHINGTON—President Donald Trump has agreed to keep U.S. troops in Syria, U.S. officials said on Wednesday, but only until the defeat of the Islamic State extremist group, a task he expects to be completed in a matter of months.

Mr. Trump’s decision, at a White House meeting, was a recognition of military commanders’ argument that they needed more time. But Mr. Trump made it clear that he expected the rest of the operation to go quickly, officials said, and sharply narrowed the scope of the U.S. involvement from the administration...