I've been a hockey fan my entire life. It recently dawned on me that hockey is the only major sport where players are punished for infractions by effectively being put in time out.
I've been digging through the historical archives to try to figure out when this form of punishment was created and why. The 1923 book How to Play Ice Hockey has this description of the penalty box:
It often happens that a player, excited by the heat of play, will commit fouls that he would never do if he was cool and considerate. Generally a short rest in the timer's box will serve to make him see the folly of his actions and how much he handicaps his team by being off the ice.
I checked to see if other sports from the turn of the 20th century used to have similar punishments. That doesn't seem to be the case. For example, Football (1896) describes punishments similar to current NFL rules.
This is a good question, and one that is harder to answer than I expected.
The "first"^1 indoor hockey game, played in Montreal on March 3, 1875, had a summary written in local newspapers the next day, which notably did not mention any penalties or anything like that. Similarly, the 1877 McGill rules, the earliest codified rules for hockey (seen here) do not mention anything about infractions either. The only related thing would be the last point: "All disputes shall be settled by the Umpires, or in the event of their disagreement, by the Referee." So those don't really help us out on anything.
However the first recorded book about hockey, Hockey: Canada's Royal Winter Game by Arthur Ferrell^2 (1899) does have a page that goes over the rules of the Canadian Amateur Hockey League, which was the top league in Canada at the time. Sec. 8 notes:
Sec. 8. The puck may be stopped but, not carried or knocked on, by any part of the body, nor shall any player close his hand on, or carry the puck to the ice in his hand. No player shall raise his stick above the shoulder, except in lifting the puck. Charging from behind, tripping, collaring, kicking or shinning shall not be allowed, and for any infringement of these rules, the Referee may rule the offending player off the ice for that match, or for such portion of actual playing time as he may see fit.
This is the first reference I can find to players being specifically sent off, a trend that continues into the modern day. That said, how did this come about?
For that I can not find a clear answer. I reached out to some other people who know hockey history, and was advised it may stem from lacrosse, which has a similar procedure for penalized players. Lacrosse as we know it today also came from Montreal, with the first modern games played there in 1867. Lacrosse is something I am not familiar enough with to get into here, so I cannot comment further on that, but the links between the two sports are well-established, and it would not be surprising if that is the case.
That said, I can also comment that a specific penalty box was not formally established until 1916; before that players were simply sent off for a while (notably goaltenders as well, until the mid-1930s; when that happened a non-goalie would usually take over for the few minutes, most famously by Lester Patrick in 1928, who was the 44-year-old coach of his team; see his photo here). Even then, it was not until 1963 that the NHL mandated separate boxes for players on opposing teams, a wise choice considering the players sent off would sometimes keep fighting (even today a shouting match and taunting are not uncommon, despite the glass partition). Sports Illustrated wrote an article in 2018 about this more recent history^3, which is definitely worth reading, though they don't go into the very early origins.
I hope that clears some things up, and if you have any other questions just let me know.