It depends.
Doing something gay is based on sexual or romantic attraction, not physical contact, so it’s only gay if you’re doing it because of an attraction for yourself or someone of your gender.
If you’re masturbating while imagining you’re having sex with someone of the opposite gender, for example, or merely for physical pleasure, then it’s not gay.