The domestic cat is descended from wild cats that were tamed twice - in the Near East and then Egypt, according to the largest study of its kind.
Farmers in the Near East were probably the first people to successfully tame wild cats about 9,000 years ago.
Then, a few thousand years later, cats spread out of ancient Egypt along maritime trade routes.
Today, cats live on all continents except Antarctica.
Scientists think wildcats began hanging around farms to prey on mice attracted to grain stores, starting the long relationship between humans and felines.
"There were two taming events - one in the Near East at the beginning and one in Egypt much later," said lead researcher Eva-Maria Geigl.
"And then the cat spread very efficiently all over the ancient world as a ship's cat. Both lineages are now present in modern cats."
From rat catcher to pet
Cats haven't always been lazy creatures, lounging around the house.
They spent thousands of years working as rat catchers on ships and farms, before becoming fully domesticated.
"I would say cats chose human company, but it was a commensal relationship - it was profitable to both sides," explained Dr Geigl, of Institut Jacques Monod in Paris.
In the study, researchers extracted mitochondrial DNA (which is passed down the maternal line) from more than 200 ancient cat remains that came from Viking graves, Egyptian mummies and Stone Age sites.
DNA evidence shows cat domestication began about 9,000 years ago in the Near East, where farming started. Farmers were probably the first people to tame wild cats and then take them on their travels, either accidently or deliberately.
A second wave of cat domestication happened in ancient Egypt. Cats spread to Europe during the Roman era and went even further during the Viking period. Egyptian cat DNA was even found in a Viking port, suggesting cats were carried on maritime trading routes to northern Europe.
Surprisingly, perhaps, tabby cats appeared only in the Middle Ages. DNA evidence suggests the gene mutation that causes blotched markings appeared in a cat in western Turkey in the 14th Century.
Over the next few hundred years, tabbies spread around the world, as cats became prized for their beauty rather than utilitarian skills.
"There was very little breeding and selection going on in cats up the 19th Century, in contrast with dogs," said Dr Geigl. "The cat was useful from the very beginning - it didn't have to be changed."
Today, there are many breeds of cats with different markings and coats. They include exotic breeds, from the Bambino (a hairless short cat) to the Cornish Rex (with a curly coat and Whippet-like body).
The research is published in Nature Ecology & Evolution.
Follow Helen on Twitter.