The Prime Directive seems to be a core principle of Starfleet and a bedrock philosophy for their interaction with less developed civilizations, yet seems to be violated with remarkable regularity. Has any Captain actually been court-martialed and/or punished for violation?
In-canon the answer is no. Captains usually tread a very fine line of morality before deciding to pull the trigger on violating the Prime Directive.
The closest a serving captain seems to have come is in "Star Trek Into Darkness" where Captain James T. Kirk and First Officer Spock save a planet's inhabitants from an extinction-inducing volcanic eruption but violate the Prime Directive by exposing the Enterprise to the natives. On returning to Earth, Kirk is threatened with a Court martial and loses command of the Enterprise with Kirk demoted to first officer and the rank of Commander.
The only guaranteed way to avoid violating the Prime Directive is to ban space exploration and diplomacy altogether. However, the Prime Directive has done a remarkable job of helping the Federation avoid the type of imperialistic tendencies that human societies have demonstrated throughout our history — especially when contrasted against other galactic powers like the Klingons, Cardassians, Romulans, etc.
Generally, when the Prime Directive is violated (often by subordinates rather than a Starfleet captain), the violations straddle the more morally thorny areas of the Prime Directive: i.e. is it really ethical to allow millions of innocent people to die in favor of allowing "history" to run its natural course? — an argument that suffers from the naturalistic fallacy as well as ignoring the possibility that Starfleet itself is part of the natural system rather than just a neutral observer.
Also, often captains will bend the Prime Directive based on technicalities rather than violating the directive outright, making it easier to justify their actions to Starfleet. (E.g. having the endangered society ask the Starfleet crew for help.)
Another common situation is to prevent or negate the interference by another warp-capable species on a less developed society. There are strong parallels here with Cold War politics, but generally Starfleet captains seem to act on a genuine desire to uphold the underlying principles of the Prime Directive, if not the actual letter of the law. It's akin to a pacifist who believes in non-aggression rather than pure non-violence.
All of this is to say, the Prime Directive is a complex issue with lots of moral and legal grey areas and room for interpretation. And when you take into account Starfleet higher ups like Admiral Ross, who directly aided Section 31 in violating the Prime Directive, it becomes less and less likely that Starfleet captains will be court-martialled every time they save a planet from natural disaster or get seen by a pre-warp civilization or free a shape-shifting space creature from alien captivity.
Of course, they still get reprimanded, no doubt, but the real court-martiallable offenses are situations like The Omega Glory or ST: Insurrection. In the former case, it's almost certain that Captain Tracey was court-martialled as soon as he was brought back to Starfleet. In the latter case, the vice admiral was killed before he could face justice. Starfleet officials who commit less serious offenses would likely only face a board of inquiry, as Captain Janeway alluded to when confronted with her own violation of the Prime Directive in Counterpoint:
We know of some captains being on trial: Kirk because of Lt. Finney’s death (who faked it), Picard on the loss of the Stargazer. Both have been cleared of all charges (so no punishment). Michael Edington (not a captain - so probably not answering your question) was tried and found guilty.
Out of universe: if a captain would be found guilty he most certainly would loose his command and we wouldn't see him starring in the series anymore. So if a captain stands trial we can expect that they violated the Prime Directive (giving the circumstances) in a proportional, reasonable way.