Your brain identifies rejection and physical pain very similarly and in both cases will release opioids to treat them both types of “pain”.
EDIT: So some of you are pointing out that opioids are drugs, not chemicals in the brain. In my defense, I did say I was skeptical about these facts in the title.
I believe the correct term should be endorphins.