While reading over World War 2, I'm reminded of just how destructive and vicious it was as a conflict. How did two of the war's primary belligerents go on to not only become economic powerhouses in their own right, but also such close allies of the United States, who had been one of the primary enemies?
WWI is sometimes regarded as the cause of WWII. Germany was punished so much in WWI that it is not surprising they would react belligerently again. Learning from these mistakes, the Allied powers decided to become more involved in the reconstruction of Germany and Japan post-WWII. America's goal through reconstruction was to destroy everything in the governments of Germany and Japan that created their fascistic, supernationalistic, and imperialistic tendencies and to reconstruct the governments to a point where they can be a functioning and beneficial part of the international order.
I don't know how much what I am about to say applies to Germany as I am more familiar with Japan's reconstruction, but I assume it does to a certain extent. In Japan, America did not have to deal with the interference of the Soviet Union which it did in Germany's reconstruction. America remade the Japanese government and constitution to be democratic and lack military elements. America specifically reconstructed Japan so that it would be a Western sphere of influence in East Asia throughout the Cold War. America helped Japan economically, especially during the Korean and Vietnam wars when forces were mobilized from there. Similarly West Germany and West Berlin benefited heavily from the Marshall Plan in which America offered monetary help to European countries in order to keep them stable during the Cold War and to promote capitalism.
I'm not very familiar with the details surrounding America's occupation of Japan and Germany. I just wanted to leave this here to give you a general idea while you wait for more refined answers. Basically, America helped out Japan and Germany so that they would be allies in the Cold War.