I think the purpose of the story of Esther is to justify the popular holiday of Purim which otherwise lacked a biblical basis and which was probably of pagan origin. The major theme of Purim was role reversal; a Purim rabbi is a student who would, for instance, replace a real rabbi, similarly a Purim king might be selected from the poor or an orphan girl promoted as Purim queen, men and women would exchange clothes, and so forth. One variation on this theme was masquerade which developed in the Middle Ages; the use of masks concealed one's identity in role-reversal games. The story of Esther and Mordecai gives an etiology for these practices. Vashti and Esther exchage places: Vashti as favored queen is requested to take the role of concubine, refuses, and is replaced by a Jewish member of the harem who becames queen. Haman and Mordecai also exchange places, with Haman killed in place of Mordecai and with Mordecai taking his place in the kingdom. The festival however likely did not originate from this story but rather derives from an older Babylonian festival called Sakaia (Sacaea), which preceded the New Year Akitu festival, which was later adopted by the Persians:
Five days, including the vernal equinox, March 21 or 22: Sacaea was an ancient five-day Babylonian New Year festival associated with Anaitis, the Syrian war goddess identified with the Greek goddess Athena. It was characterized by drunkenness and licentious behavior as well as a reversal of the usual customs and relationships. Slaves ruled their masters throughout the festival, and a mock king was selected from among the criminals. After being feasted and honored for five days, the mock king was executed, thereby serving as a surrogate for the real king, who was supposed to die each new year when a new king was born.
The Babylonian origin of Purim is suggested by the fact that the name derives from Akkadian pûru "lot" (probably from a feature of the celebration itself, as opposed to the story of Haman). The story of Esther aimed to give a "Jewish origin" to what the Jews in Persia and Babylonia had adopted from the culture they lived in, and the narrative itself seems to draw on the typical "court intrigue" tale with Haman taking the role of Nadab/Nadin in the tale of Ahiqar, or the role of three nobles conspiring against Daniel in ch. 6 of Daniel. There is also a resonance with the events of 522 BC pertaining to the accession of Darius to the throne. There was an imposter Bardiya who usurped the throne around the time Cambyses died and he ruled for several months before Darius assassinated him; a member of pseudo-Bardiya's harem, Hutaosa (Atossa in Greek), was key to uncovering the fraud and she became queen under Darius. The true identity of the imposter was Gaumata the Magian and a slaughter of Magi followed, resulting in a national holiday:
"So the Persians when they heard of that which had been brought to pass by the seven and of the deceit of the Magians, thought good themselves also to do the same, and drawing their daggers they killed the Magians wherever they found one; so that if night had not come on and stopped them, they would not have left a single Magian alive. This day the Persians celebrate in common more than all other days, and upon it they keep a great festival which is called by the Persians the festival of the slaughter of the Magians, on which no Magian is permitted to appear abroad, but the Magians keep themselves within their houses throughout that day" (Herodotus, Historiae 3.79).
This has a resonance with the theme of genocide and mass killing in the Purim story, and there is a superficial (non-etymological) similarity with the Persian name Hutaosa/Atossa and Esther's Hebrew name Hadassah. There is also a superficial similarity between the Sakaia festival's promotion of a slave to the role of king, followed with punishment, and the situation involving the imposter king who then was executed by Darius. It makes me wonder if there is a chance that there is a conflation between two separate Persian festivals that the story draws on: the Sakaia festival (giving rise to the theme of role reversal in Purim) and the Magophonia festival mentioned by Herodotus and Ctesias, which commemorated (if the Hellenistic writers are to be believed) a mass slaughter.