This is really just for a similar comparison, but this answer I wrote comparing the difference in post-1945 aid to West Germany and development aid to Africa might be of interest.
Japan in 1945 was not an exact analogy to Germany, but they both shared quite a few similar features: a highly educated, literate population, a diversified industrial economy, and a history of parliamentary politics. Also, after 1945 neither were involved in wars, especially civil wars. These are features that they wouldn't have shared with post-colonial/developing countries like the ones mentioned.
Not to discourage any further answers, but OP, you may be interested in these wonderful answers by u/ReaperReader and u/ParkSungJun on why Japan‘s Economy recovered after WW2.
https://www.reddit.com/r/AskHistorians/comments/aswqd9/why_did_japans_economy_see_massive_growth/
Hopefully others can chime in on what role the US may or may not have in Japan‘s transition back to a democracy, as well as other nation-rebuilding efforts where US intervention has failed and why they did.
Please take note that we have decided to let this question stand as to make it possible to give an answer on what made the occupation of Japan successful in terms of turning it into a democratic country. At the same time, please keep the 20 year rule in mind.
While awaiting a more detailed response, I'd like to just recommend Dower's "Embracing Defeat" if you're interested on an in-depth dive into postwar Japan. It's really, really good.
I would definitely contest your assertion it was "on course for stable economic growth" within just a few years; true stability was not achieved until the mid-1950s, and substantial investment was required to fuel this boom. Japanese society was truly on the verge of total economic and social collapse in the postwar era, and that cannot be understated.
I really cannot do justice to the 'solutions' to Japan's dilemma and the failures to nation-building in Iraq and Afghanistan, there's so much involved beyond my knowledge, but something that struck me in Dower's writing was the attitude of the Japanese people towards occupation that simply cannot be compared to the expectations and desires of the Iraqi and Afghan people.
To the Japanese population, they were a defeated and conquered nation exhausted after years of a very valiant and intensive war effort. This was not a change of governance or a 'liberation', they were being occupied, at gunpoint, by a hostile government who had totally broken their backs through overwhelming brute force. Their nation had surrendered, the war had been fought and lost. The idea of relitigating it was... not very popular.
People certainly were scared and ashamed and felt humiliated by the defeat and occupation, but imperial propaganda had taught them to expect the worst. For America to have come in, built an ostensibly free society (through very authoritarian means, mind you), and grant relief and aid to the Japanese people was not expected.
There was a certain degree of cautious embracement in many quarters, especially among the working class, to American occupation, once it revealed itself as somewhat benevolent (if totally intolerant of dissent). There was a growing belief that life could now return to something close to normal, and many more liberally minded were hopeful that the corrupt imperial cabal of officers and bureaucrats, who'd lived lavishly in a time of great struggle, might be finally done away with. This fueled collaboration and complacency.
In Afghanistan and Iraq, these material and political improvements were not only promised, but demanded by a population yearning for sovereignty. They were demanded by the international community at large, really. America were not the victors in a struggle for national survival, they had toppled regimes they claimed unjust, in the mission of building just regimes. This set expectations among the populations being occupied. There was no such expectation in Japan.