Dogs and humans became best friends in Ice Age Europe between 19,000 and 30,000 years ago, say scientists.
That was when wolves, ancestors of domestic dogs living today, were first tamed by ancient hunter gatherers, according to new genetic evidence.
The findings challenge a previous theory that dog domestication happened some 15,000 years ago in eastern Asia, after the introduction of agriculture.
In reality, the history of the bond between dog and man appears to go back much further, to a time when fur-clad humans were living in caves and hunting woolly mammoths.
Scientists used a tried and trusted technique of DNA analysis to establish what populations of wolves were most related to living dogs.
DNA from domestic dogs most closely matched that extracted from the fossil bones of ancient European Ice Age wolves, as well as modern wolves.
There was little similarity with DNA from wolves, coyotes and dingos from other parts of the world.
Early tamed wolves may have been trained as hunting dogs or even protected their human masters from predators, the researchers believe.
The Finnish and German team wrote in the journal Science: "Conceivably, proto-dogs might have taken advantage of carcasses left on site by early hunters, assisted in the capture of prey, or provided defence from large competing predators at kills."
Dog domestication of a "large and dangerous carnivore" was likely to have occurred partly by accident, possibly after wolves were attracted to hunter camp sites by the smell of fresh meat.
The research contradicts previous thinking that early farming brought wolves sniffing around villages, leading to them forming relationships with humans.
"Dogs were our companions long before we kept goats, sheep or cattle," said Professor Johannes Krause, one of the researchers from Tubingen University in Germany.
The scientists analysed a particular type of DNA found in mitochondria, tiny power stations within cells that generate energy.
Unlike nuclear DNA found in the hearts of cells, mitochondrial DNA is only inherited from mothers. This makes it a powerful tool in tracing ancestry.
The study included genetic data on 18 prehistoric wolves and other dog-like animals, as well as 77 dogs and 49 wolves from the present day.
Among the prehistoric remains were two sets of German dog fossils, one from a 14,700-year-old human burial site near Bonn, and the other dating back 12,500 years from a cave near Mechernich.
Most of the DNA from modern dogs was traceable to just one lineage, closely related to that of a wolf skeleton found in a cave in northern Switzerland.
"I was amazed how clearly they showed that all dogs living today go back to four genetic lineages, all of which originate in Europe," said study leader Olaf Thalmann, from the University of Turku in Finland.