What it does is potentiate the pain-killing effects of opiates while simultaneously suppressing the nausea that is often a side effect. Vistaril (hydroxizine) is often used for the same purpose.
And it can mess you up - when I was in labor with my second, they gave me Phenergan because I was puking so much. That shit hit me so hard that I kept falling asleep between contractions, and seeing as they were two to three minutes apart, I was fairly impressed.