How does a chocolate pudding cake work?
I don't know that there's anything magical going on here.  The batter is initially more dense (and thus sits on the bottom), but it contains baking powder.  Baking powder generates gas, and the gas output increases as the temperature rises in the batter -- so gas output will become greatest once the batter is already beginning to solidify a bit.  Gas bubbles in the batter then cause it to become less dense, so the batter floats to the top of the liquid.

It's the same sort of thing that causes cakes to rise normally.  The only difference here is that you add some liquid and deliberately don't mix it in.  But thick batter will absorb external liquid very slowly.  (Think of what happens if you have a pancake batter that's too thick and try to correct it by adding liquid; you need to mix it in because gluten and protein networks hold the batter together.  And as batter begins to cook, the starches swell, making it even harder for more liquid to penetrate.  Think of trying to add flour to hot gravy and getting "clumps.")  Cocoa powder also repels water (because it contains fat, which, like oil, doesn't mix well with water), and having that on top of the mixture before pouring in the water may also have some effects in maintaining separation.

So, you have a liquid and a thick batter that don't mix well.  As the batter bakes, it simultaneously becomes lighter (due to gas bubbles forming from the baking powder) and harder for the added liquid to penetrate.  Thus, the denser liquid settles to the bottom, and the baked "cake" ends up on top.  The liquid may also begin to simmer during baking, which would serve as further agitation to push up any remaining bits of solid batter to the top.  The liquid obviously does absorb some cocoa powder, sugar, as well as starch from the flour over time from the outer layer of the batter, which thickens it enough to form a sauce or even a custard-like bottom.  The thickening of the liquid on the bottom is likely enough to maintain separation and prevent the liquid from gradually soaking into the top layer even after baking.