Whether you knew it or not when posting, this is actually a legal history question. John Doe has been used to denote a fictitious legal party in English-speaking common-law jurisdictions since the 1600s. Other common names for fictitious parties include Richard Roe and William Stiles.
Why John Doe was chosen for this purpose is frankly a matter of conjecture. It has been noted that Doe is a fairly uncommon surname in England, so it was unlikely actual litigants bearing that name would be encountered. This suggests why we don't have an imaginary John Smith, for example: too many real ones. There's also a theory that the last name was conflated with the verb "to do." John Doe "does" something in the litigation. Jane Doe seems simply to be the feminine version of the given name.
Originally John Doe was not so much an unknown person as a completely fictitious, nonexistent person who was conjured into existence to aid with litigation procedures. For English common law consisted of a number of distinct "actions" which had their own rules. The most celebrated example of this is the use of the action in ejectment to determine possession of real property. The proper course to make a claim that you had the right to possess land that someone else was possessing was the venerable assize of novel disseisin. Novel disseisin was procedurally very complicated, took a long time to resolve, and favored the defendant. Plaintiffs would have preferred the new-fangled type of action generally called trespass. But you could not use any type of trespass for this purpose since novel disseisin was still in existence.
Clever lawyers came up with a subterfuge. Instead of directly suing the other claimant, the person claiming rights in the land would pretend to have leased those rights to John Doe. He would then pretend that John Doe had been kicked off the property by William Stiles, the fictional tenant of the other claimant. In a dispute between competing tenants, John Doe did have the right to bring an action in ejectment, a type of trespass action, against William Stiles. Even though these tenants were fictitious, the actual parties in interest would be allowed to argue in court. Courts and litigants allowed the subterfuge. The Doe v. Stiles form of ejectment action appeared in the mid 1600s and remained in use until the mid 1800s, when legal reforms made it unnecessary.
In fact, entirely fictitious parties was largely phased out starting around this time and are rarely encountered anymore. Instead, John Doe and his friends appear in court in two main sets of circumstances:
Use of John Doe and derivatives to refer to unknown persons in contexts outside litigation follows from the above. This is how unidentified corpses came to be known as "John Does."
A good introduction to John Doe in civil litigation is provided by Carol Andrews, "Meet John Doe," University of Pittsburgh Law Review 57 (1995).