I think a lot of it goes back to the nature of the belligerents and what a loss looks like for the losing side. There was like a spell being broken in both Germany and Japan when their WWII era governments surrendered. I’m not nearly as well informed on the Japanese side, but I’ve heard it likened almost to a death of a culture. People openly wondered how the Emperor could have surrendered, and how surrender was not inferior to simply fighting to the death. But the culture of obedience to the Emperor, especially in the military, meant the vast majority of Japanese soldiers would not consider continuing to fight against his wishes–note there is a famous coup attempt that was attempted late in the war after the atomic bombings, to “secure” the Emperor and make sure the war effort continued. But aside from that of the millions of Imperial Japanese forces virtually all of them laid down their weapons and went home.
One of the great, perhaps the greatest spirits of national militarism lost its heart over night, and the effects continue to this day in Japanese society.
In Germany, German practicality kicked in pretty quickly, recognizing that the Nazi Party was done for and never coming back, the vast majority of ordinary Germans main concern was “okay how do we get our country fixed and back to running”, it was basically a giant heap of burning rubble, and much work needed to be done. These attitudes were very conducive to being done with war.
In wars like Afghanistan and Iraq, the target of U.S. military force (the Taliban in Afghanistan and the Saddam Ba’athists in Iraq) were just the top dogs in a complicated system in which lots of groups below them hated each other and wanted to kill each other. With the Taliban significantly weakened from the days when it controlled 90% of the country, and Saddam and the Ba’athists completely destroyed, all those groups that hated the fuck out of each other decided it was time to start killing each other ASAP while the time was ripe. It’s just a very different cultural situation than in Japan or Germany.
It helps that Japan and Germany are and were cohesive nation states. Iraq and Afghanistan never have been. Afghanistan is a historical region, but not a “nation.” No ethnic group calls its Afghan or Afghani, the country is 42% Pashtun and 27% Tajik, and then the remainder is made up of various smaller ethnicities. Even more, these groups are very tribal, so just saying “Pashtun” only has certain connotations. There are plenty of Pashtun tribes that don’t get along with each other, for example.
Iraq is less racially plural–being overwhelming Arab aside from the Kurdish region in the north, and the Turkish population also in the North, but Iraq is a religiously divided country. There have been serious issues with its Shia and Sunni populations being willing to live in a country where the other “side” has political power, which is not a good recipe for a viable country. The fact that powerful Sunni and Shia neighbors funnel money to the respective sides doesn’t help. It’s honestly a miracle Iraq isn’t in open civil war right now to be honest, and that may just be due to general war fatigue after a solid 12-14 years of almost endless fighting culminating in the multi-year ISIS battles.