It is difficult to say how it first came about, but some key factors can be used to determine.
First, the main difference between sexual and asexual reproduction is that sexual reproduction “reorganizes” the parent DNA into a new pattern, while asexual is essentially the parent self-cloning. By being able to reorganize DNA, sexual reproduction had an advantage over asexual reproduction because it caused more mutations to occur in the DNA. This leads to a higher chance that a mutation will be beneficial to the organism, especially because the baby organism gets two sets of DNA to choose from, doubling its genetic resources. It also means that if one parent has a successful mutation that the other parent does not have, the baby organism will have a better chance of acquiring that mutation.
Second, we know that with two (or more) sexes, one sex must be more involved in the reproduction than the other. In most species, this is the female organism, which can do the reproductive work of bearing young. The male organism can merely contribute DNA but does not actually bear young. With 50% of the population unable to make the babies, conditions must be good enough to allow the other 50% to reproduce often enough and quickly enough to maintain the whole species’ population. This means temperature, food sources, and safety from predators or environmental threats need to be ideal.
We can break down the evolution of sexes a bit more; there is the term “anisogamy”, which means reproduction with sperm and egg. This first evolved in tiny cells that contained only one set of chromosomes. Chromosomes are chains of DNA. When the cells would reproduce, the egg would provide one chromosome, and the sperm would provide a different chromosome. For a brief period, the baby cell would contain both chromosomes. After a short period of development, one of the chromosomes would “win”, and that chromosome would become the DNA pattern that the new cell would eventually pass onto its own young.
This “anisogamy” developed when two compatible cells evolved, which was slightly different but able to mate with one another. In the first tiny cells that accomplished this, they were too similar to be called “male” or “female,” but the difference was enough to spark the eventual evolution of male and female. The evolution of separate sexes led to the eventual development of more and more complex processes, where the chromosomes from both contributors were mixed and matched. The final form of reproduction came to be the one in which both halves of the DNA were contributed by each parent, which were then combined and became the new DNA of the offspring. This came about first in prokaryotes.
According to the theory of evolution where all life forms came about from a single prokaryotic cell, the development and more and more complex forms of reproduction kept evolving in the newer organisms. Their anatomical and physiological forms changed accordingly until the first animals were capable of carrying an offspring within their bodies. Therefore, mammals are the highest form of animals eventually developed the feature of sexual reproduction.