I believe it originated in Croatia sometime in the middle ages where it was primarily worn by men to show allegiance to their King. It seems to have gained popularity in France where the French form for Croatia resulted in them being called 'cravat'. Details in the article.
Why do men wear neckties?