Hiroshima and Nagasaki didn’t, but other detonations did, like Castle Bravo.
The Castle Bravo detonation was both 1. orders of magnitude stronger than the Hiroshima and Nagasaki bombings and 2. took place *on the ground*. Little Boy and Fat Man were detonated about 500m above ground level.
Castle Bravo was an oopsie
To maximize their effect you want to detonate a nuclear weapon in the air over the target so more of its energy goes to smashing buildings and very little goes to digging a hole. Fat Man detonated about 500 meters above ground level so it didn’t really spend much energy digging a hole
During nuclear testing though, sometimes weapons were placed on towers but if they didn’t completely vaporize their tower it would require cleanup like in Upshot-Knothole Ruth. The Operation Castle series was all done at ground level or on a relatively short structure, the tallest was a 4 meter tall barge.
Sooo why did Castle Bravo dig a big hole that wasn’t particularly expected? Because it was wayyy stronger than expected!
Castle Bravo was expected to be 6 MT and leave a crater similar to the 11 MT Ivy Mike test at Enewetak which left a crater roughly 1 mile wide, but ended up leaving a 1.5 mile diameter crater when it turned out to be 15 MT and not just 6 because it turns out Lithium-7 is a very potent fusion fuel
A lot of later tests were done on barges or at sea to avoid vaporizing test islands and because land tests got boring
Why does an explosion on the ground cause a crater but one high enough up in the air do not?
The answer is the same for a firecracker as for a nuclear bomb. Put a firecracker on sand and you get a small crater but a bit up in the air and there is no crater. Nuclear bombs work the same just different heights needed.
Castle Bravo was a detonation on the run because is simple to do a test with a 10 tonnes bomb where the goal is to show a new design works. So a gound detonation was simplest to do.
The nuclear bombs over Hiroshima and Nagasaki were designed to do maximum damage to the city. The optimal altitude was for Hiroshima 580 meters (1,900 ft) and quite similar for Nagasaki that has a bit more powerful with another design.
The Trinity test a bomb just like the one used on Nagasaki puts it on top of a 100-foot (30 m) tower. The result was a 5-foot (1.52 m) deep and 30-foot (9.14 m) wide blast crater.
To dig a crater you more or less have to have the fireball actually in contact with the ground. A weapon detonated high enough that the fireball does not contact the ground will not leave a crater. The blast wave is powerful enough to knock down houses and buildings, but that is still not strong enough to excavate a hole by itself. The fireball, however, can vaporize the ground, and has immense pressures and temperatures, and that is enough.
Hiroshima and Nagasaki were high airbursts; the fireballs never came even close to the ground. Bravo was a huge device detonated on the ground itself. Trinity was a bomb the size of Hiroshima and Nagasaki, detonated on a not-very-high tower, and did leave a modest crater (which was about the size of the fireball).
It’s a choice of the person detonating the bomb.
In Japan, the US wanted to damage the largest area with “enough” damage to knock down buildings. An air burst provides high pressure over a larger area, and no crater.
In other situations, you can apply the “most damage” to the ground by setting off the nuke right on it. This was done with a relatively short tower in the Trinity blast, leaving a crater of the glass-like mineral trinitite.