The key idea here is that a small initial difference between the sexes can be self-enhancing: selection can start with an initial, slight difference and make it grow larger and larger, until the As become what we now call males, the Bs what we now call females. The initial difference can be small enough to arise at random. After all, the starting conditions of the two sexes are unlikely to be exactly identical.