In once case it was prior to the pain being inflicted -- for a scheduled medical procedure.
One could easily resolve the question of whether opioids cause euphoria by doing a double-blind clinical study. I would love to do that study, in fact.
[1] http://www.rxlist.com/oxycontin-drug/side-effects-interactio...
Or maybe you're right: they just take away pain. But it turns out a lot of us have existential pain so it works all the time.