It's in your mind, friend. You have made that equation (masturbation = upset stomach). Here are some steps:
1. Accept that it is not a physical issue and is psychosomatic.
2. Stop worrying and relax.
3. Convince yourself that masturbation is a pure form of natural pleasure and it doesn't harm you in any way.
4. Do some light yoga.
5. Take some precautionary medicine (like 'Bismuth Subsalicylate' or Famotidine if you get acidity or diarrhea).
6 Masturbate
Believe me, you don't have to take the precautionary medicine every time going forward, just for a few times initially. You will be out of this.
If you can't help yourself, consult a psychologist. Best if luck!