Masturbation was an unintended consequence of developing a drive for sexual pleasure. For some reason it is only a problem when human civilization first came about, because people needed many babies, that's why masturbation was so discouraged and we developed cultures where it was so stigmatized.
For animals in the wild there probably isn't a problem of chronic masturbation such that it would impede on one's ability to produce children because... well... I don't really know why. But I assume generally in a wild setting the theraputic benifits of masturbation must outweigh (in terms of survival benifit) the lessened drive to reproduce with a female, therefore in such a setting it was generally good for survival.