The decision, announced Friday, came after Iraq failed to agree to legal immunity for a small residual force that Washington had hoped to keep in the country to train the army and counter the influence of neighboring Iran, officials said.