Aren’t we faced with the same fundamental mystery of human insight? Where does it come from?
As far as wolves, it's still an open question as to how much they "self-domesticated" before humans noticed and started working from the other end. Seeing how human encampments would produce delicious food scraps in refuse (and still do, just ask any black bear!), it stands to reason that some animals would try to cash in on that opportunity. Then you have a selection process whereby the ones that are too skittish would not even bother, and the ones that are too aggressive would be chased away or worse.