Maybe there really is something called ‘instinct’.
Nowadays, we may say some behavior is ‘hardwired’ into that person or animal. As with Border Collies when raised on a ranch. Some breeds just seem to know (without any formal training) that children or an injured person would need to be protected.
Either that, or the dog knew he was lost, and staying with the child was better than being fully alone.
Dogs would have to be invented if they weren’t already there for us.
I’m sure there are also stories of Heroic Cats too.
Cats can intimidate a foe just by their Kabuki Dance type of behavior when they want to. Sometimes I hear their shrieks during the night, when a pair of Tom Cats face off for the Battle Royale.
Once my old cat Jake (RIP Jake) was sitting under my truck when a dog went after him. My young cat Lara (who did not care for Jake) came out of the bushes, leaped into the air and landed on the dog’s face sending the dog running. Once had a cat named Junior (RIP Junior) who went after a German Shepherd that was barking at my Niece. Cowed the shepherd he did. Cats can be heroes, but only on their terms.