WASHINGTON—President Trump ordered a rapid withdrawal of all U.S. military forces from Syria, officials said Wednesday, marking an abrupt shift of the U.S.’s posture in the Middle East.

The U.S. immediately began moving a handful of personnel from Syria and will quickly extract about 2,000 forces over the next few weeks, officials said, ending a four-year military campaign against Islamic State on the brink of its defeat.

“The...