The Atheist Perspective

They didn't. Sexual reproduction began without sexually distinct organs, and then such organs evolved in tandem over time. Many plants, for example, are hermaphroditic: each plant has both male and female sexual organs. But the first sexually reproducing organisms were a form of single-celled bacterium which thus had no organs at all. As sexually reproducing organisms evolved into multicellular forms and their cells began to specialize, some cells began specializing for male and female reproductive function, both evolving at the same time, by small gradual steps.

Evidence suggests gendered organs probably began as organs in the same individual (as in many plants), then the capacity evolved in some species for individuals to become gender diversified by their environment (as in many reptiles, some developing male organs, some female), and then finally these gender differences became locked into DNA as a chromosome mutation. All this occurred long before any mammal existed. Thus, the very first mammals were already genetically evolved to have fixed genders, and already had matched gendered organs in place, as those organs had evolved long before (from fish through reptiles). Human sex organs are just evolved versions of their mammalian ancestors' sex organs, which is why our genitalia are almost identical to those of apes and monkeys. And mammalian sex organs are just evolved versions of the sex organs of their ancestors, the reptiles, which is why mammals have genitalia most similar to reptiles (in contrast to birds, insects, or fish, for example).

It's worth adding that the implication of the question being asked is that sex organs have to "fit" each other, which is actually a late development in animal history. Fish, for example, never even come into contact. The male organs merely inseminate eggs that have already been laid by the female. This procedure gradually evolved into the system most commonly found in reptiles, where the male inseminates the eggs while still in the female, originally relying on only a very crude fit between genitals. But once this had begun, a better and better fit evolved. Then some of these reptiles evolved away the need for an actual egg and thus started giving live births, and from them mammals evolved, which is why we don't lay eggs. Hence the "fit" between human genitalia is a feature that has gradually evolved over eons, and had already been well developed even before mammals appeared (much less people).

Evolution of Sexual Reproduction