I think it's a general sign of mutual affection: 'I like you and feel comfortable enough with you to rest my head on yours' and vice versa. You'd never see a tarantula doing it though, so perhaps not quite universal.
Some, but not all mammals have a sense of 'ego' where they are aware of themselves as a separate entity and recognise others of their kin as 'similar' if not trustworthy. Mammals also tend to build up a bond within their groups more so than others do (ie arachnids/ amphibians/ reptiles), and so head resting could be seen as affection, trust, greeting, or in younger ones - reassurance. I've seen it many a time with: horses, apes, dogs, cats, rodents, cows, sheep etc.
If you take humans for example, we have our instinctive actions from birth eg suckling, grasping etc etc, then as we develop we take on 'learned' behaviours such as: 'shaking hands' as a sign of respect or 'waving' to say goodbye - these are not instinctive, they are acquired skills. The Head resting in animals could be perhaps likened to 'hugging' in humans - instinctive from birth to be close to the mother to stay safe, but later on in life it develops into a sign of affection with friends & Family/ people we admire/ partners etc. as although it is no longer a necessity, we like doing it. It makes us feel good. The mammals who have the higher level of self-ego would probably be more likely to show the head resting behaviour as they show signs of 'feeling' and connecting with their kin more than others.
I am no psychologist, but it would be nice to think the animals you see doing this when restful are just 'having a hug' :o)