This is one of the classic, garden-variety “skeptical” questions, usually designed to ridicule the Bible and/or the alleged gullibility and incredulity of Christians.
The prominent Protestant apologist, Josh McDowell (who was, incidentally, crucial in sparking my own initial interest in historical apologetics back in 1981), dealt with this question:
Genesis 5:4 tells us that Adam had sons and daughters. At first, sons and daughters of Adam and Eve had to marry each other to populate the earth. Cain probably married a sister or niece or grand niece.
Assuming the accuracy of the Genesis account, and considering the length of lives recorded (around 900 years, on the average), a very sizable population could have developed very rapidly . . .
Moreover, the Scriptures nowhere indicate at what points in the life of Cain he murdered his brother, married his wife or built his city. Even a few hundred years might have passed before all of the events took place. (Answers to Tough Questions Skeptics Ask About the Christian Faith, with Don Stewart, San Bernardino: Here’s Life Publishers, 1980, 98-99)
In fact, they (the critics) are often the illogical ones, not the biblical writers. I’ve seen it even in philosophy professors, in my many debates, as an instance of what C. S. Lewis called “chronological snobbery.”
Logically speaking, we are not forced at all (as McDowell indicates) to face a difficulty of Cain being the only man on earth besides Adam, and Eve being the only woman, at the time that Cain married. One cannot logically infer that with certainty from the text itself. Instead, we have simple statements made, separate from chronology. Critics have simply assumed this, which leads (fallaciously) to much of the resultant “problem.”
We often don’t understand this (as well as the different forms of Hebraic literature) in interpreting the Bible, superimpose our modern, more “Greek” thinking on the Bible, and conclude that the people were ignoramuses who didn’t have a clue.
But in the text itself, we find that Cain shows an awareness that there are other people “out there” when he is exiled: “whoever finds me will slay me” (Gen 4:14).
If he hadn’t assumed that, why would he even say this or worry about it? Since the time frame of his murder of his brother is not given, we are not required to accept that it happened when there were only four human beings on the earth. We know that Adam and Eve “had other sons and daughters” (Gen 5:4).
I think the more difficult question, then, is the question of how incest was morally neutral at this time, and later forbidden as immoral. One might hold (I think) that there was some “leeway” initially in this matter and that it was not a moral absolute, similar to the way in which concubinage was more accepted in the earlier Old Testament, whereas later on, a single wife was the norm, and what God required.
It has been argued by some that the corruption of the gene pool produced the results that we now see in incestuous relations, but that this would not have been present initially. I’m no expert on this second question, by any means, but that is how I view the matter, anyway, for what it’s worth.