Magnesium will increase ATP. And ATP is the energy that is needed to sensitize G protein coupled receptors. One of those G protein coupled receptors is the opioid receptor.
So what magnesium is doing is making your opioid receptors more sensitive to your natural opioids.