The origins of captain’s authority comes from maritime law and tradition. A ship’s captain had absolute authority and was the unquestioned commander responsible for the ship, cargo, and crew. The same concept in aviation was applied in the early years, before 1930. Aircraft captain‘s absolute authority became essential after the advent of flying boats. Early regulations, gave the pilot in command (PIC) -while in flight – the final responsibility and authority of the aircraft and crew members.
So during the flight the aircraft captain is basically a god. If the situation requires, he/she can deviate from any rule to ensure safety of the passengers and the crew members. For example, the PIC can arrest a troublemaker, they can initiate a force landing to drop off a certain passenger or change the direction of the destination if necessary. An important detail is that as soon as the plane lands, captain must report every single action they have taken and explain why they thought it was neccessary.
However, all the trouble that emerged during the flight must be investigated by the police or other legal institution.