Interestingly enough, the Warspite, a WW1 battleship, and the Scharnhorst, using "only" 11" guns, were roughly tied with each other for longest naval gunfire hits in combat @ ~26,000 yards. Warspite scored her hits against Guilio Cesare (who I think had those long range 15" guns we mention above) recorded as 26,000 yards. Scharnhorst scored her hits against the Glorious alongside Gneisenau, calculated to be around ~26,400 yards. They opened fire @ 28,000 and based on the recorded time of hits and firings the longest hit would have been around 26,400ish yards. Given the accuracy of rangefinders, charts, and timekeeping there is some margin of error, so one could say that Warspite and Scharnhorst have a dead heat for hitting a moving target.
If you include stationary targets there's some evidence supporting Massachusets hitting the Jean Bart while it was at anchor with distances out to 29,000 yards.
And if you really want to see some internet browbeatings, start googling on Japanese claims for Yamato making hits @ something like 34,000 yards, I believe at Samar. Lots of flak about Japanese overinflation, but there is some merit to the claim. Another detraction from that claim I think was that a near miss caused damage to one of the escort carriers, but it wasn't a direct hit. Shell splashed short but continued under the keel and exploded on a delayed fuse (as designed), knocked out machinery and cause the White Plains to swerve out of formation.
The real fun stuff is cruisers making hits out past 18k yards, like Prinz Eugen vs Hood with"dinky" little 8" guns.