The details I remember are too vague to Google... apparently. The story surrounds the crew of the Enterprise discovering that the leader of the village where they're visiting (I forget why) is actually a Starfleet Captain who has abandoned his command and is aiding the aboriginals in violation of the Prime Directive. I'm pretty sure it's not Private Little War, nor A Taste of Armageddon, but I'm open to being convinced. I don't remember any names or other specific details, only that (spoiler for Star Trek Beyond): ...it's essentially the same bad guy as in Star Trek Beyond. I'll update with any details I remember...
Could be "The Omega Glory":
Kirk: Captain's log, supplemental. The Enterprise has left the Exeter and has moved into close planet orbit. Although it appears the infection may strand us here the rest of our lives, I face an even more difficult problem – a growing belief that Captain Tracey has been interfering with the evolution of life on this planet.
Star Trek Season 2 Episode 25: "The Omega Glory"
The episode finds the Enterprise discovering an abandoned starship, the Exeter, in orbit around a planet, nearly the entire crew having been killed by an unknown alien disease.
The lone survivor is the ship's captain, Captain Tracey, who is living on the planet1 among the "Kohms", one of two technologically-primitive indigenous tribes. Having gone slightly nutty as a result of being the only survivor of his crew, Captain Tracey has decided to use his advanced Starfleet technology to assist the Kohms against their rival tribe, the Yangs.
1 Because for some reason, staying on this one planet prevents the disease from spreading. It's Star Trek, what do you want?
Could this be "TOS: The Paradise Syndrome"?
Trapped on a planet whose inhabitants are descended from Northwestern American Indians, Kirk loses his memory and is proclaimed a God while the crippled Enterprise races back to the planet before it is destroyed by an asteroid.
Perhaps the episode you are looking for is 'Whom Gods Destroy' centering on an ex Starfleet captain (Captain Garth) who has taken control of a mental institution and holds Kirk and Spock prisoners. http://memory-alpha.wikia.com/wiki/Whom_Gods_Destroy_(episode)