WASHINGTON — The United States military is spending about $1 million to help detain thousands of Islamic State fighters and their family members in makeshift camps run by Kurdish militias in northern Syria, pulling the Pentagon deeper into the war zone detention operations it has sought to avoid.

The dilemma is unfolding even as President Trump has pledged to withdraw the 2,000 remaining United States troops in Syria, many of whom are vetting the most dangerous detainees, and suspend more than $200 million in State Department recovery funds for the country.

Defense Department and Kurdish officials said several thousand detainees — including at least 400 fighters from more than three dozen countries and their families, as well as other Syrian militants — were being held in several camps. The American funding is paying to erect fencing, put bars on windows and otherwise secure schools and other buildings being used as temporary jails for fighters who were captured or surrendered after last year’s collapse of Raqqa, the Islamic State’s self-proclaimed capital.

Military officials insist American personnel are not strengthening the buildings or guarding the detainees themselves, just paying for it.