Thanks for your query!
Good news is that you don’t seem to have an organic erectile dysfunction given spontaneous night time erections and when you try to masturbate.
Although I would need much more elaborate history from you before pin pointing the exact diagnosis but it sounds like you may be going through what is called as functional ED. Common causes include performance anxiety (very common, especially in setting of upcoming marriage), issues with the current sexual partner, or some other emotional problem. Psychological counseling is the preferred therapy in this setting. Medications have been seldom used to treat it (although with positive results).
Next Steps
I would recommend you to reach out to your psychiatrist/psychologist skilled in psychotherapy to help with this issue.
Health Tips
In the mean time try these:
- Physical exercise/ cardio work out has shown a lot of promise in functional ED: it helps increase blood flow to all the organs which in turn may help with sustaining useful erection.
- Practice self relaxation techniques including deep breathing, 20 mins prior to intended sexual activity with a partner.