Up to World War 2, formal declarations of war was the standard way to start a war, at least between major powers.
When did this practice start and why? Since when did it become the norm?
Well at least there was such practice in the days of the Romans and ancient Greeks. Up to the Troy war. We do not have earlier sources, but arguably the practice originates thousands of years BC.
Of course "formal" meant "verbal and explicit" in those times, not necessary written on the paper.
I suppose the practice originated with ultimatums: the attacking party usually suggested a peaceful settlement of the conflict, in exchange for some tribute or other actions. It is always more beneficial to get things done peacefully (under the threat of force) than in the course of war.