We didn’t treat them as pets until we started getting much more civilized, but we’ve had dogs for quite a while. There’s some debate about whether they basically ‘self-domesticated’ by following nomads for scraps, sort of like remora fish, and the humans decided that they were useful; most researchers believe that cats self-domesticated by eating the rats attracted to grain stores, which was obviously useful to humans but didn’t give us as much control over their food source. In either case, dogs and cats who didn’t waste energy being terrified of humans were at a selective advantage in a low-calorie environment.
There’s also potentially a sort of mutual domestication going on, with humans turning over some of our sense of smell and becoming entirely helpless in the presence of puppies and kittens. It’s been a mutually advantageous relationship.