The Senate voted 68-23 in favor of a non-binding amendment, drafted by Republican Majority Leader Mitch McConnell saying it was the sense of the Senate that Islamic militant groups in both countries continue to pose a "serious threat" to the United States.
The procedural vote to cut off debate meant that the amendment would be added to a broader Middle East security bill likely to come up for a final Senate vote next week.
The amendment acknowledges progress against Islamic State and Al-Qieda in Syria and Afghanistan but warns that "a precipitous withdrawal" without effective efforts to secure gains could destabilize the region and create a vacuum that could be filled by Iran or Russia.
It calls upon the Trump administration to certify conditions have been met for the groups' "enduring defeat" before any significant withdrawal from Syria or Afghanistan.
Thursday's Senate action marked the second time in two months that the Republican-led Senate has supported a measure going against Trump's foreign policy, although legislation to change his policies has yet to become law.
In December, over Trump's objections, the Senate voted to end U.S. military support for the Saudi-led coalition in the war in Yemen and blame Saudi Crown Prince Mohammed bin Salman for the killing of journalist Jamal Khashoggi.
However, the December measure went no further, since it never came up for a vote in the House of Representatives, then led by Trump's fellow Republicans.
The potential impact of Thursday's vote was similarly uncertain, since the amendment McConnell offered was non-binding and there has been no indication of when, if ever, the broader Middle East security bill would come up for a House vote.
Trump has decided to withdraw 2,000 U.S. troops from Syria on the grounds that Islamic State militants no longer pose a threat, saying on Twitter on Wednesday, "We have beaten them" as he disputed Senate testimony by his director of national intelligence, Dan Coats, on Tuesday that the group still posed a threat.
Trump said earlier on Thursday he would bring American troops home if a peace deal were reached to end 17 years of war in Afghanistan.
The United States and the Taliban have sketched the outlines for an eventual peace accord, a U.S. special envoy said on Monday, but there was no sign the insurgent group had accepted key U.S. demands.
Want to enjoy 'Zen' reading - with no ads and just the article? Subscribe todaySubscribe now