It began with the populace being stricken by lice and syphilis. They became fashionable when Louis XIV started wearing one, and now it’s just a persistent tradition.
I was about to suggest, as a guess only, that it might been to prevent the spread of lice- back then it seems that folks didn’t treat the disease, but only the symptoms, ie, person stinks to high heaven because of filthy sanitation? Don’t clean up, just keep dumping more perfume on. (We actuslly had a neighbor like that once- she wasn’t all there, but incredibly held a very high position in a ,ocal hospital- it was so bad that when the house was demolished, they found dead cats in the house, feces everywhere, the f3ces would get so thick, then she would buy more linoleum and place it over the old feces. The house was a nightmare situation.)