It's a part of maritime law. Ship captains were given broad authority over legal matters while at sea for a few reasons, like keeping moral up and keeping the sailors disciplined and orderly, but also simply because no one else was available. On a 6 month voyage at sea, you have no government officials, no preachers, no police, no lawyers, etc. So the captain took those responsibilities, typically. And what began as a practicality and then turned into tradition, was eventually standardized into maritime law.
Here is a very interesting article on the history of maritime law. Hopefully this will answer your question better than I did.
https://www.abyssinialaw.com/study-on-line/item/1072-historical-development-of-maritime-law