This is not like an official declaration of war. It's more like a right to self-defense. But there are other things behind this. Such as some groups in Iraq attacking American bases. This triggers a defensive counterattack impulse in the US military. Secondly, there are rumors that the US, together with Gulf countries, will send troops to some islands in the Persian Gulf. Although Iraq's relations with these Gulf countries seem good, one must remember that Iraq invaded Kuwait back in the day. So we shouldn't consider their relations to be too good. Military activity on the other side of the Gulf and the danger of territorial gains also create a defensive reflex within Iraq against this.
Personally, I found Trump's idea of a ground offensive to be beyond stupid and utopian, as he is trying to hide the fact that he has completely lost the war through various fallacies. For me, this is certain; he cannot wage this ground war, it's completely out of the question. However, it is likely that Iraq does not see it the way I do. Therefore, a defensive reflex had already been triggered there, and this accidental American attack has fired the gun.