So masturbating, or not masturbating, by itself, is neither good nor bad.
By itself, it probably isn't going to completely prevent you from getting prostate cancer, and a lot more goes into those chances - family history, age, genetics, etc., are probably more important than masturbation. Lots of men who masturbate regularly get prostate cancer.
Why did you stop masturbating? Biologically and medically, there isn't anything wrong with it. It's normal. You may have religious reasons for not doing it, which are your own and I won't address.
But also biologically, it's pretty normal to have "lustful thoughts" - that's your sex drive, and that's biologically driven. Again, some religions have some thoughts about that, and I won't address that, since we're a medical site, but you can have thoughts without acting on them with another person.
You can read more about ejaculation and prostate cancer here - https://www.webmd.com/prostate-cancer/ejaculation-prostate-cancer-risk#1 There's still a lot we don't know. It doesn't prevent some of the deadlier, more aggressive forms of prostate cancer, for example, but may help prevent some others. We don't know why.
If masturbation hasn't been a problem for you - if it hasn't been so frequent that it's affecting other areas of your life, for example, or it's not a religious issue for you, then there's no reason to stop doing it. If you feel better not doing it, preventing prostate cancer isn't reason enough to start doing it again.