As it turns out, the phrase makes perfect sense when taken in the proper historical context. It all goes back to the early meaning of the verb “do”, which has been used since the 14th century to mean “prosper, or thrive”. So, in the 15th and 16th centuries, people would ask each other “How do you?” in the same sense as they ask each other today “How are you”? However, at the time, the phrase “How do you” was used literally, as a query about health, and not as a greeting. The change in usage from a specific query to a general greeting was gradual, and the greeting usage did not become widely used until the 18th century.
OK, but why the second, final “do”?
Here there is no absolutely definitive answer, but a probable one. By the 18th century, when “How do you?” had become a commonplace greeting, there had been a change in the accepted form of expression. For example, the medieval “wither goest thou” became “where are you going”. So “How do you” sounded antiquated, and was “updated” to “How do you do”.
So the origin of the formal greeting “How do you do?” is clear, yet it begs the question, why did this query about another’s health evolve into a greeting? Using a wide-ranging question as a greeting seems awkward, since the one querying is not really expecting an actual answer, but instead a meaningless positive response and a reciprocal query/greeting. Imagine if, at a formal event, you’re asked “How do you do?”, and you actually respond truthfully about the state of various aspects of your health! Even when we use an actual greeting, such as “hi”, “hello”, or even “howdy” (which interestingly is probably a contraction of the early “how do ye”), it’s invariably followed by an insincere query or two. The greeting process thus becomes a greeting, followed by a few meaningless queries and rote answers before the actual conversation begins.
Interestingly, many science fiction aliens and future humans simply and elegantly greet by actually saying “Greetings”, implying that science fiction writers feel we will one day move on from our almost comical greeting rituals. Only time will tell.
[I found most of the material regarding the origin of the phrase “How do you do?” in this article on The Phrase Finder website.]