It does seem utterly counterintuitive that a vehicle that cannot balance by itself would be able to balance with the addition of a heavy rider elevating its center of gravity. Usually making an object top-heavy renders it even more unstable, and bicycles are not stable to begin with.
And adding a rider on top of a bicycle does make it more unstable, but the amazing flexibility of the human brain allows us to transform that instability into stability, kind of like how modern fighter jets are intentionally unstable to make them more manoeuvrable.
Bicycles are a marvel of physics and biology working together.