There are many theories regarding the origin of sex (production of genetic variations and so on). However, none of these theories are entirely satisfactory because it is unclear what aspect of sex is advantageous and how the mechanism works and has remained stable for millions of years.
I think that sexes origin is a natural consequence of partially directed evolution: in the presence of a priori information, gene exchange (actually, the exchange of information) is advantageous.
See, for example, Melkikh A.V., Khrennikov A. 2016. Quantum-like model of partially directed evolution. Progress in Biophysics and Molecular Biology. http://dx.doi.org/10.1016/j.pbiomolbio.2016.12.005.
Article Quantum-like model of partially directed evolution