There is some controversy actually on whether the US had to drop the bombs or not.
By that stage in the war, the US had Japan completely surrounded and cut off from supplies/oil/etc. Many people have said Japan would have surrendered in a couple of weeks anyway.
Some have speculated the real reason to drop nukes on Japan was to send a message to other countries like Russia that the US was the new "big dog".
There is also some evidence that Russia was planning to move on Japan, so the US decided to move first and decisively. Using nukes would deter the Russians.