BAGHDAD — Iraqi Prime Minister Haider al-Abadi is in talks with the Trump administration to keep American troops in Iraq after the fight against the Islamic State group in the country is concluded, according to a U.S. official and an official from the Iraqi government.

Both officials stressed that the discussions are ongoing and that nothing is finalized. But the talks point to a consensus by both governments that, in contrast to the U.S. withdrawal in 2011, a longer-term presence of American troops in Iraq is needed to ensure that an insurgency doesn’t bubble up once Islamic State, also known as ISIS, is driven out.

“There is a general understanding on both sides that it would be in the long-term interests of each to have that continued presence. So as for agreement, yes, we both understand it would be mutually beneficial. That we agree on,” the U.S. official said.

Both officials spoke on condition of anonymity in line with regulations.

The talks involve Secretary of Defense Jim Mattis and Iraqi officials over “what the long-term U.S. presence would look like,” the American official said, adding that discussions were in early stages

U.S. forces in Iraq would be stationed inside existing Iraqi bases in at least five locations in the Mosul area and along Iraq’s border with Syria, the Iraqi government official said. They would continue to be designated as advisers to dodge the need for parliamentary approval for their presence, he said.

He said al-Abadi is looking to install a “modest” Iraqi military presence in Mosul after the fight against the Islamic State group is concluded along with a small number of U.S. forces. The forces would help control security in the city and oversee the transition to a political administration of Mosul, he said.

The U.S. official emphasized that there were no discussions of creating independent American bases in Iraq, as such a move would require thousands more personnel. He said the troop levels would be “several thousand — similar to what we have now, maybe a little more.”

The Pentagon currently has close to 7,000 U.S. troops in Iraq, many not publicly acknowledged because they are on temporary duty or under specific personnel rules. The forces include troops training Iraqi forces, coordinating airstrikes and ground operations, and special forces operating on the front lines.

The news comes as Iraqi forces are struggling to push Islamic State fighters out of a cluster of neighborhoods in western Mosul that mark the last patch of significant urban terrain the group holds in Iraq, nearly three years after the militants overran nearly a third of the country.

During a visit to Iraq in February, Mattis and Lt. Gen. Stephen Townsend, the top U.S. commander in Iraq, described an enduring partnership between the U.S. and Iraq.

“I imagine we’ll be in this fight for a while and we’ll stand by each other,” Mattis said.