A cargo vessel involves four interested parties, the master, the crew, the ship owner and the owner of the cargo. On the earliest commercial voyage likely all four parties were a single person.
During the middle ages shipowner, master, crew and cargo owner all financed the voyage and shared the profits, if any. However the master was second in command subordinate to the shipowner. While the master had technical responsibility for the safety of the shi,p for example the selection of the route, but was often overridden by the owner seeking higher profits.
As shipowners came to own more then one ship, owners began to stay ashore, sending a supercargo to sail with the vessel and handle the commercial side while the master's role expanded to include stores and hiring crews. Over time the master took over the duties of the supercargo and became the sole authority aboard ship.
During the age of monarch, kings were thought to be responsible only before God. This philosophy became the basis to consider the master to be "Next to God Master of the Ship" During the 19th century as society changed the master was no longer viewed as being next to god, the last trace being in 1902.
During the Golden Age of Shipping, from about 1850 to the First World War vessels remained independent of shore side control and masters enjoyed a high status. Many become wealthy shipowners themselves.
After the First World War communications with vessels at sea began to improve and, as communications improved, shoreside control become possible and the master's authority diminished.
Today it is questionable if the ship master has sufficient authority to operate safely given intense commercial pressure, overbearing port officials and an increasing tendency to criminalize mariners.
Source: The Legal Position of the Ship Master