BAGHDAD – Defense Secretary James Mattis made an unannounced visit to Iraq's capital on Monday to reassure allies of the U.S. military's commitment to support the sprawling operation to recapture the city of Mosul from entrenched ISIL militants.

Ahead of his arrival, however, Mattis made it clear that he did not advocate President Trump's oft-stated wish to take Iraq's oil. Such an undertaking would be illegal and require decades of occupation by hundreds of thousands of troops, and likely cost more money than could be earned from the oil.

"We're not in Iraq to seize anybody's oil," Mattis said during a stop in Abu Dhabi.

Trump, as a candidate and as president, has repeatedly said that the U.S. should have "taken Iraq's oil," including at CIA headquarters a day after his inauguration last month.

"The old expression, 'to the victor belong the spoils' — you remember," he said. "I always used to say, 'Keep the oil.' I wasn't a fan of [the war in] Iraq. I didn't want to go into Iraq. But I will tell you, when we were in, we got out wrong.

"If we kept the oil, you probably wouldn't have ISIS because that's where they made their money in the first place. So we should have kept the oil. But OK. Maybe you'll have another chance. But the fact is, should have kept the oil," he said, using a different acronym for ISIL, the militant group that seized oil fields in Iraq and Syria and sold their output on the black market.

Iraq's economy is nearly entirely reliant on oil and it remains the lifeblood for Iraqi Prime Minister Haider Abadi's fragile government as it tries to provide basic services to citizens and maintain the nation's aging infrastructure.

Legal experts have said the U.S. seizure of Iraqi oil would have violated decades of international law, including the Geneva Conventions.

And Sabeh Al Noman, a spokesman for Iraq's counterterrorism services, suggested that Trump's remarks on oil, at least, had caused no lasting damage. Asked in a news conference in Baghdad whether Iraq was worried that the Trump administration would try to steal its oil, he replied, "We will trust your government, so we won't worry about that."

When Mattis stepped off the C-17 cargo plane Monday, it marked his first return to the country where he spent years in combat as a Marine Corps officer before retiring as a four-star general in 2014.

He had face-to-face talks with Abadi and other senior Iraqi government officials, whom he called "our partner in this fight" against ISIL. Iraqi ground forces began the assault Sunday to retake Mosul, Iraq's second-largest city. The operation, backed by U.S. air power and special forces, is expected to take months.

"We're going to make certain that we have good shared situational awareness of what we face as we work together, fight alongside each other to destroy ISIS," Mattis said ­Sunday before the trip.

Iraq is also one of seven countries named in Trump's temporary ban on travelers that was put on hold by the courts. The ban caused anger in Iraq, where members of parliament considered retaliating by refusing to grant visas for U.S. nationals.

While Trump intends to issue another version of the ban, Mattis said he was promised that it would shield the thousands of Iraqi interpreters, advisers and others who have assisted the U.S. military in Iraq.

The New York Times contributed to this report.