The high frequency of same-sex sexual behaviors (SSB) in free-ranging animals is an evolutionary puzzle because fitness benefits are often unclear in an evolutionary context. Moreover, the physiological and genetic underpinnings of SSB remain unclear. We exploited an extraordinary natural experiment to examine the impact of environmental factors (local sex ratio [SR]) and testosterone (T) levels on SSB in a dense population of Hermann’s tortoises monitored for 7 years. Under the combination of high density and extremely skewed SR (~50 females, >1000 males), males courted and mounted other males more frequently than females. They even exhibited extravagant sexual behaviors, attempting to copulate with dead conspecifics, empty shells, and stones. T levels remained within the species’ normal range of variation. SSB was not observed in other populations where SR is not, or less skewed, and where density is lower. This study reports the first natural example of a “prison effect,” whereby a high population density combined with female deprivation triggered SSB as a mere outlet of sexual stimulation. More generally, it supports the hypothesis that SSB can be a nonadaptive consequence of unusual proximate factors rather than reflecting physiological disorders.