Dogs are smarter than they often get credit for. At least some of them are. Like humans, of course, some are clearly smarter than others.
And we’ve been breeding them to love humans for at least 12,000 years. Maybe a lot longer. It shouldn’t be too surprising when something like this occasionally happens.