Updated: Aug 9, 2020
Because of their ancient origin and their unique structure and function we do not know how viruses originated. We do know just about them, about genetics, and about the structure and function of existing life forms, to make reasonable estimates, although there is no better evidence for one carefully analyzed estimate than for others. The earliest estimates were that viruses exist as an early form produced by the accumulation and eventual bonding of primordial elements. As protein and nucleic acids were better understood, this estimate was joined with ideas that viruses originated as parts of early cells, and that they were extruded (or escaped) from those cells with sufficient composition and structure to persist. This latter idea gains credence from the obligatory parasitic mode of virus reproduction, and their inability to take in nourishment and metabolize as do all other forms of life.
We may never have the final answer, but this is one of those good questions that keep scientists seeking the answer, and in their search coming up with important discoveries about the origin and development of life.
The origins of viruses in the evolutionary history of life are unclear: some may have evolved from plasmids—pieces of DNA that can move between cells—while others may have evolved from bacteria. In evolution, viruses are an important means of horizontal gene transfer, which increases genetic diversity in a way analogous to sexual reproduction. Viruses are considered by some biologists to be a life form, because they carry genetic material, reproduce, and evolve through natural selection, although they lack key characteristics (such as cell structure) that qualities, viruses have been described as "organisms at the edge of life", and as replicators.
Sources: Crawford DH (2011). Viruses: A Very Short Introduction. Oxford University Press, US. pp. 16. ISBN 978-0-19-957485-8.
Wu, Katherine J. (15 April 2020). "There are more viruses than stars in the universe. Why do only some infect us? – More than a quadrillion quadrillion individual viruses exist on Earth, but most are not poised to hop into humans. Can we find the ones that are?". National Geographic Society. Retrieved 18 May 2020.
Rosen FS (October 2004). "Isolation of poliovirus--John Enders and the Nobel Prize". The New England Journal of Medicine. 351 (15): 1481–83. doi:10.1056/NEJMp048202. PMID 15470207.