Despite the legal implications and concept being much older, the notion, as far as I know, is a fairly modern one, dating back no earlier than the mid-19th Century. Specifically, it arose from Victorian era ethics and notions of responsibility. Another one that gained traction around the same time was "women and children first," which was generally attributed to the HMS Birkenhead in 1852 (and later popularized by Rudyard Kipling). However, the concept of "the captain goes down with the ship" has no specific event or law or writing that can be pointed to as its origin. What we do know is that, while the concept had been around for decades, the idiom really gained fame and popularity after the sinking of the RMS Titanic in 1912.