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).
Comments
Mammals didn't evolve from reptiles, but from amniotes (animals that lay their eggs on land).
Amniotes split in two branches, Synapsida and Sauropsida. The synapsida group eventually gave rise to modern mammals, while the sauropsida group eventually lead to modern day turtles, reptiles and birds.
The evolutionary distance between humans and modern reptiles is just as large as the evolutionary distance between humans and modern birds.
Similarities between the sex organs of humans and modern reptiles can be the result of selection pressure, preserving an established organ structure, or could be the result of convergent evolution.
Since the amniote-split took place approximately 320 million years ago, the chance of ever finding fossilized soft parts, such as sex organs is very small.
You keep using the word "randomly" as if that answers everything. Development of nerve endings in genitalia that cause sexual excitement will have evolved randomly over the millennia, certainly, but the action of natural selection means that creatures with a genetic predisposition to possess such nerve endings will be more likely to procreate, and therefore pass on the genes responsible, due the pleasurable nature of sexual activity.