The victorious allies imposed a punitive peace on German at the end of WWI. This caused huge resentment, that was at least partly responsible for the rise of Nazism and WWII. The Marshall plan was, I believe, an attempt to the stop the same cycle from happening again. Similarly for Japan. It was an act of incredible generosity by the USA, but I think history shows that it was also a very smart investment. I can't imagine such generosity and foresight being employed again any time soon.
I think the Cold War played a role too. Germany was defeated, but its industry was still fairly operational. In contrast to housing, which was bombed into pieces, German heavy industry was remarkably operational until the very end and the British were surprised to find that German factories were, on average, equipped with more modern machines than British ones.
And some 75 per cent of that industry was in Allied hands. It made strategic sense to rebuild the country in face of a Soviet threat and make it a factory for the Allies (notably, the German army was only reconstituted much later, in 1955) instead of destroying it.