It's because it's a feature 0 people will complain about adding if it all works well. It's a small, minor, completely unnecessary detail.
If it's there, then someone had the time, the energy, and the freedom to add it, just because they wanted to. It's not an item that will make it on the checklist of a minimum viable game.
To add to the other responses, I think there's maybe a sense of relief or novelty in it, as the rest of many of these games are intense and/or violent. Having a little wholesome moment in the midst of all that really stands out.
It goes way further back than Wikipedia claims (2019). Hell, CoD: Ghosts made a huge deal about your canine companion's interaction when they announced it, and I remember petting the dog was a meme back then (2013).
It's on the timeline around amusing 4th wall breaking idle animations and attacking a neutral character invoking a deadly/game-ending response tropes. I'm sure there were even text based games where animals just served as a way to waste a player's turn. The main one I remember is Black & White because it actually had some learning algorithms that trained your pet to be good or evil. Interesting game for the time.