Is it known how the first viruses formed?
One of the main points of contention in the study of virus evolution is whether or not they appear before the last universal cellular ancestor (LUCA) or afterwards (commonly accepted: genes that "escaped" from host organisms aka the escape hypothesis or vagrancy hypothesis).  Basically though, the LUCA is the most recent ancestor that all organisms living on Earth are derived from. 

Increasing evidence support that ancestry of viruses may predate the LUCA, for example an idea is that there "was an 'ancient virus world' of primordial replicators that existed before any cellular organisms and that both RNA (first) and DNA (later) viruses originated at this time, donating some features to the first cellular organisms.  However, "a competing theory is that RNA cells existed before the LUCA and that RNA viruses were parasites on these RNA cells that later evolved DNA as a way of escaping host cell responses."

So basically, some theories support the pre-LUCA ancient virus origin and some support post-LUCA.  Because it's impossible to replicate conditions from billions of years ago, several approaches using evolutionary genetics and probability models are recommended by the author of the paper, referenced below.  He also discusses the specific supporting criteria for the theories that you can read in greater detail.

Holmes, E. C. (2011). "What does virus evolution tell us about virus origins?" J Virol 85(11): 5247-5251.