BAGHDAD — Acting defense secretary Patrick Shanahan highlighted the U.S. commitment to Iraqi autonomy during talks in Baghdad on Tuesday, in the wake of comments from President Trump that have jeopardized plans for an ongoing counterterrorism presence there.

Shanahan made his first-ever visit to Iraq as part of an inaugural overseas tour after being tapped in January to become acting Pentagon chief. That followed the sudden departure of Jim Mattis, who resigned over differences with Trump.

“I wanted to make clear to them that we recognize our role. We understand that we’re there by invitation, that we jointly share the resources. And that we clearly recognize their sovereignty,” he told reporters of his discussions in Baghdad with Iraqi Prime Minister Adel Abdul Mahdi.

Shanahan’s visit occurred as the Trump administration seeks to wind down the operation against the Islamic State, the extremist group that took over large swaths of Iraq and Syria in 2014. It also follows a political outcry generated by Trump’s recent suggestion that he might want to maintain a military presence in Iraq to “watch Iran,” his administration’s chief adversary in the Middle East.

Trump, speaking in an interview with CBS, suggested he might keep troops at bases in Iraq “because I want to be looking a little bit at Iran, because Iran is a real problem.”

The characterization, which differed from what previously had been described as a plan to keep U.S. troops there to guard against the Islamic State’s resurgence, prompted some Iraqi politicians to seek passage of legislation that would limit Washington’s role.

Speaking to reporters later in the day, Shanahan said he discussed Iran only indirectly with Abdul Mahdi, who said his government needed to maintain good relations with its neighbors and the United States.

The question over the future of American forces in Iraq was first raised early last year after Baghdad declared military victory over the Islamic State. But the issue grew into a frequent talking point during Iraqi elections in May. There are about 5,000 U.S. troops in Iraq, supporting local forces as they seek to ensure that remaining Islamic State cells cannot stage another full-fledged insurgency.

Several candidates aligned with tickets that have a historically combative relationship with a U.S. presence in Iraq argued the troops were no longer needed and that the United States should keep at most a small number of advisers in the country to continue to train Iraqi security forces.

In December, Trump’s unannounced visit to American forces in western Iraq turned the issue into a national obsession. Many Iraqis were aghast that the U.S. president had not met with the country’s leadership when he was in the country, saying it was an affront to Iraq’s sovereignty. Trump’s more recent comments drew fresh calls for the expulsion of U.S. forces and condemnation from Iraqi President Barham Salih and former prime minister Haider al-Abadi, both considered close allies by Washington, as well as Iraq’s top Shiite cleric.

Several political factions that are usually at odds with one another, including those backed by Shiite cleric Moqtada al-Sadr and a coalition representing several Iran-aligned militias, have called for fast-tracking a bill that would recommend limiting the number and activity of U.S. forces in Iraq when parliament reconvenes next month.

U.S. officials also have been considering shifting some American troops from Syria to Iraq after Trump unexpectedly announced in December that he would withdraw all 2,000 U.S. service members there. Shanahan, who spoke to reporters during a later stop in Brussels, said that topic was not discussed in Baghdad.

His brief visit to Iraq, following a one-night visit to Afghanistan, takes place as the White House considers nominating him to become Trump’s second defense secretary.

The selection of Shanahan, who has served as deputy Pentagon chief since mid-2017, would be a shift, given that he is a relative newcomer to military policy.

He is the first person serving in the role since the Sept. 11, 2001, attacks who had not visited either Iraq or Afghanistan — the countries whose counterinsurgency wars have consumed the military’s attention for nearly two decades — before taking over.

On Tuesday, Sen. James M. Inhofe (R-Okla.), chairman of the Senate Armed Services Committee, suggested that he would not support Shanahan’s nomination. Asked about the comments, Shanahan said he would be “happy to serve the country in any capacity the president asked me to do.”

Shanahan, who will meet in Germany this week with other Western countries battling the Islamic State, will be among the U.S. leaders responsible for ensuring that militants cannot make a comeback once American troops depart Syria. Military officials have said the Kurdish-dominated Syrian force that has been the main U.S. partner there needs significant outside support before it can do so alone.

“Risk can be high, and risk can be low. So I think people’s concerns is just — over the course of history, they’ve seen how difficult it is to eradicate terrorism,” he said before reaching Iraq. “The follow-on, the support and security . . . that will be an important part of the discussions [in Europe].”

An announcement is expected within weeks that U.S.-backed Syrian forces have recaptured final areas of eastern Syria controlled by the Islamic State.