I can't answer whether it was a nightmare or a flashback for certain, but were you ever raped or sexually abused that you remember? If not, it's possible something happened (not necessarily sexual abuse or rape, but having someone - especially a caregiver - make you feel uncomfortable sexually could be enough to cause your problem) you are not yet ready to confront. That's what happened to me. It's been many years since I've had nightmares about sex.
Luckily, I have a very patient husband that has helped me get over it so that sex is now enjoyable for me.
Do you see a therapist? If not, this could be your 'wake-up call' to find one. Otherwise, I hope you are sharing this with him/her.