A study of dog DNA has shown that our "best friend" in the animal world may also be our oldest one.
The analysis reveals that dog domestication can be traced back 11,000 years, to the end of the last Ice Age.
This confirms that dogs were domesticated before any other known species.
Our canine companions were widespread across the northern hemisphere at this time, and had already split into five different types.
Despite the expansion of European dogs during the colonial era, traces of these ancient indigenous breeds survive today in the Americas, Asia, Africa and Oceania.
The research fills in some of the gaps in the natural history of our close animal companions.
Dr Pontus Skoglund, co-author of the study and group leader of the Ancient Genomics laboratory at London's Crick Institute, told BBC News: "Dogs are really unique in being this quite strange thing if you think about it, when all people were still hunter gatherers, they domesticate what is really a wild carnivore - wolves are pretty frightening in many parts of the world.
"The question of why did people do that? How did that come about? That's what we're ultimately interested in."
To some extent, dog genetic patterns mirror human ones, because people took their animal companions with them when they moved. But there were also important differences.
For example, early European dogs were initially diverse, appearing to originate from two very distinct populations, one related to Near Eastern dogs and another to Siberian dogs.
But at some point, perhaps after the onset of the Bronze Age, a single dog lineage spread widely and replaced all other dog populations on the continent. This pattern has no counterpart in the genetic patterns of people from Europe.
Anders Bergström, lead author and post-doctoral researcher at the Crick, said: "If we look back more than four or five thousand years ago, we can see that Europe was a very diverse place when it came to dogs. Although the European dogs we see today come in such an extraordinary array of shapes and forms, genetically they derive from only a very narrow subset of the diversity that used to exist."
An international team analysed the whole genomes (the full complement of DNA in the nuclei of biological cells) of 27 ancient dog remains associated with a variety of archaeological cultures. They compared these to each other and to modern dogs.
The results reveal that breeds like the Rhodesian Ridgeback in southern Africa and the Chihuahua and Xoloitzcuintli in Mexico retain genetic traces of ancient indigenous dogs from the region.
The ancestry of dogs in East Asia is complex. Chinese breeds seem to derive some of their ancestry from animals like the Australian dingo and New Guinea singing dog, with the rest coming from Europe and dogs from the Russian steppe.
The New Guinea singing dog is so named because of its melodious howl, characterised by a sharp increase in pitch at the start.
Greger Larson, a co-author from the University of Oxford, said: "Dogs are our oldest and closest animal partner. Using DNA from ancient dogs is showing us just how far back our shared history goes and will ultimately help us understand when and where this deep relationship began."
Dogs are thought to have evolved from wolves that ventured into human camps, perhaps sniffing around for food. As they were tamed, they could then have served humans as hunting companions or guards.
The results suggest all dogs derive from a single extinct wolf population - or perhaps a few very closely related ones. If there were multiple domestication events around the world, these other lineages did not contribute much DNA to later dogs.
Dr Skoglund said it was unclear when or where the initial domestication occurred. "Dog history has been so dynamic that you can't really count on it still being there to readily read in their DNA. We really don't know - that's the fascinating thing about it."
Many animals, such as cats, probably became our pets when humans settled down to farm a little over 6,000 years ago. Cats were probably useful for controlling pests such as mice, that were attracted by the waste generated by dense settlements. This places their domestication in cradles of agriculture such as the Near East.
"For dogs, it could almost have been anywhere: cold Siberia, the warm Near East, South-East Asia. All of these are possibilities in my mind," Pontus Skoglund explained.
The findings have been published in the journal Science.
Follow Paul on Twitter.