These alternate names for the Messiah were eventually used by the rabbis to designate two Messiahs, in order to explain the two seemingly conflicting missions and fates which were foretold by the Prophets. In most cases, "ben Yosef" (freely interchanged with "ben Ephraim" or simply "Ephraim") was the one who would suffer and die for sin, and be rejected by His generation, as in the earlier quote; "ben David" (or just "David") was to rescue Israel from her enemies, be exalted with G-d's glory and sit on the throne of David forever.
However, there are interesting cross-overs. Some passages (such as Midrash Konen) identify the suffering One as "Moshiach ben David who loves Jerusalem." Others can be found which speak of "Ephraim" as "the" (only) name of G-d's Messiah (Pesiqta Rabbati 161a). The writers of the Zohar (I, 25b) concur that Messiah ben Yosef will also be descended from David; this implies the right of one man to both titles.
Rabbinic literature documents the debate as the sages struggled to reconcile the glorious, unending kingdom of Messiah ben David with the suffering, rejection and death said to await Him. The conjectures about these missions and the timing of their appearances vary, but the teachings agree that Messiah ben Ephraim (the mission of suffering and death) must come first.
According to Israeli scholar Dr. Raphael Patai, the splitting of King Messiah into two persons did not take place until the Talmudic period, and it had another purpose besides resolving His two missions. It was also meant to address a teaching established by this time, that the Messiah was perfectly prefigured in Moses, and reconcile it with the fact that Moses died before finishing his mission of leading Israel into the Land (Messiah Texts, p. 166).
In spite of ben Yosef's role that clearly included being killed, there were some who asserted that He would not die (Sa'adia Gaon, Emunot veDe'ot; Ben Yehoyda on Sanh. 98; the Gra, Kol Hator). More often, however, the sages who taught about Messiah ben Yosef believed that He would be raised from the dead, in order to vindicate His undeserved suffering - called to life either by G-d Himself or by Messiah ben David. One Scriptural support for Messiah's resurrection is Psalm 16:9, as explained in the following Midrash:
' לכן שמח לבי -- Therefore my heart is glad' (Ps. 16:9): [glad] in the words of the Torah; ' ויגל כבודי -- and my glory rejoices': in King Messiah who will come out of him [David]. As it is written [Isa.4:5]: ' כי על-כל-כבוד חפה -- And over all glory is a canopy' . ' אף-בשרי ישכן לבטח -- My flesh also will dwell in safety': after its death. R. Isaac Melamed said that neither corruption nor worms had power over him [David].' - Mid. Tehilim 16
Explaining David's expectation of not seeing corruption as based on his rejoicing in King Messiah, it should be clear from the Midrash (as well as from logic, since David had long been dead) that the second "David" is the Messiah, who would be raised from the dead to show that "neither corruption nor worms had power over him". This was the apostle Peter's conclusion as well, based on the same Psalm and the same logic (Acts 2:24-32).
In the next verse we see clearly that David was also talking about his own future redemption from Sheol because of G-d's Holy One, who would not "see corruption":
כי לא-תעזב נפשי לשאול לא-תתן חסידך לראות שחת תודיעני ארח חיים
For You will not leave my soul in Sheol [the grave]; You will not give up your godly one [Heb: gracious or righteous one] to see corruption; you will announce to me the path of life. - Ps. 16:10-11
Amazingly, the above Midrash goes on to explain the above reference as not applying to only David and the Messiah, but to G-d Himself:
' לא תעזוב נפשי לשאול לא תתן חסידך - 'You will not leave my soul in Sheol; You will not give up Your Hasid [Godly One]': The Holy One, blessed be He, is called 'Hasid', as it is written [Jer.3:12]: ' כי חסיד אני נאם יי - For I am gracious [Hasid], says Hashem'. And David called himself 'Hasid', as it is written [Ps.86:2]: ' שמרה נפשי כי חסיד אני - Preserve my soul [or, my soul is preserved], for I am godly [Hasid].'
The Midrash omits the end of the verse ("give up Your Hasid to see corruption") because it's clear that G-d Himself cannot die or see corruption. But its message is that Messiah, who descends from both G-d and David, who is identified with them so closely as be called by their names, would "see corruption" if G-d did not rescue Him. This idea of Messiah being called "G-d" is uncomfortable only to rationalist Jews influenced by Rambam; we have already seen ample evidence that other rabbinic schools took it in stride.
Those who taught that it would be Messiah ben Yosef who would be raised from the grave, and not Messiah ben David, faced a problem in using this passage as support; both the Psalm and rabbinic comment link it to the son of David. And actually, the recognition that the Prophets and early sages spoke of only one Messiah caused at least one later Midrash (first published around 1740) to conclude that only one would appear after all:
If they [Israel] have not acquired [merits], Messiah ben Ephraim will come; and if they have acquired [merits], Messiah ben David will come. - Nistarot R. Shim'on ben Yohai, Beit HaMidrash 3:80
R. Shimon ben Yohai (a disciple of R. Akiva) presents the same "if" that was offered by Rabbi Yehoshua ben Levi to reconcile Messiah's coming gloriiously in the clouds (Dan.7:13-14) with His coming humbly on a donkey (Zech.9:9). (See BT Sanhedrin 98a of the Talmud.) Yet the context for the above statement is the Last Days, and in the Nistarot story both of these Messiahs appear anyway, without connection to Israel's merits.
Mysteriously, their roles and Israel's responses to them are reversed. Messiah ben Ephraim performs the works popularly expected of Moshiach, and he is loved. Messiah ben David has only G-d's endorsement, and he is nearly stoned. Israel's reason for rejecting him is attributed to this confusion over one vs. two Messiahs:
And Messiah son of Joseph will sprout for them and he will bring them up to Jerusalem, and will build the Temple and offer sacrifices; and fire will descend from heaven and consume their offerings.... And Messiah son of Ephraim will die there [in Jerusalem in war], and Israel will mourn him.
And after this the Holy One, blessed be He, will reveal to them Messiah son of David. And Israel will want to stone him and said to him, 'You spoke a lie, for Messiah was already killed and there is no other Messiah still to arise.' And they will despise him, as it is written [Isa.53:3] 'נבזה וחדל אישים - He was despised and rejected by men'. - Nistarot.
The mystery deepens as both Messiahs fail in their missions. It is ben Ephraim who gathers the exiles to Jerusalem and builds the Temple; yet he dies without establishing the kingdom, and (as the Midrash tells it) Israel is scattered into the desert. Meanwhile, ben David's name implies that the throne of David should be his, yet he does not reign either, due to Israel's rejection. But the suffering described in Isaiah 53 is applied only to ben David, not to ben Ephraim. The midrash continues that the Son of David is then "hidden", but it isn't explained where or how. And not only does the Midrash fail to explain these riddles; it goes on to describe yet another appearance of Messiah:
And in the trouble caused to them, to Israel, they return and cry out from hunger and thirst; and immediately the Holy One, blessed be He, is revealed to them in His glory; as it is said [Isa.40:5]: ' וראו כל בשר יחדו - and all flesh shall see [G-d's glory] together.' And King Messiah will sprout there, as it is written [Dan.7:13-14]: 'וליה יהיב שלטן ...וארו עם ענני שמיא - And he came with the clouds of heaven... and to him was given dominion.' And he will puff at that wicked Armilus and slay him; as it is written [Isa.11:4] ] וברוח שפתיו ימית רשע - And with the breath of his lips he will slay the wicked.' - Nistarot
This is the Messiah who finally slays Israel's enemies, after which G-d regathers Israel and brings down the heavenly Jerusalem. Thus we are left with more questions than answers. Is this the first Messiah who "sprouted" but died, or the second Messiah who was "revealed" by G-d Himself but was rejected? How do two failed Messiahs morph into one "King Messiah"? And what does it mean that Messiah "will sprout" (a second time) only when and where G-d reveals Himself?
The only solution that makes sense of the puzzle is that one Messiah does it all: first coming to Israel, gathering her lost ones and building a spiritual house for G-d; then dying and being resurrected to appear again, only to be rejected as a false pretender; then being hidden; and finally returning in the clouds to slay Israel's enemies, clothed in G-d's glory and openly bearing His Name. When does He finally become "King" Messiah? When Israel realizes that they had wrongly sent Him away as a liar, and they "cry out from hunger and thirst" for a revelation of G-d.
And I will pour out on the house of David and the inhabitants of Jerusalem a spirit of grace and pleas for mercy, so that, when they look toward me about him whom they have pierced, they shall mourn for him, as one mourns for an only child, and weep bitterly over him, as one weeps over a firstborn.
What is the cause of the mourning? […] One explained, The cause is the slaying of Messiah the son of Joseph. […] It is well according to him who explains that the cause is the slaying of Messiah the son of Joseph, since that well agrees with the Scriptural verse.
Babylonian Talmud, Seder Mo’ed, Excerpt of Sukkah 52a6
Jewish tradition sometimes refers to two Messiah (מָשִׁיחַ, Mashiach) figures. Both are involved in delivering G-d’s people from exile (which is a result of sin) and ushering in the long-awaited Messianic era. Typically, when the term Messiah is used on its own, it is thought to refer to the Messiah coming as a redeemer who would bring the millennium of the Messianic Age and reign as king. However, when the teaching of the death of Messiah (מָשִׁיחַ, Mashiach) became established in Judaism as a result of several biblical teachings and prophecies, this did not fit in with the Messiah coming as a king.
This king is called Mashiach ben David (מָשִׁיחַ בֶּן־דָוִד, Messiah son of David), to show that he is a descendant of king David and that he will reign like David.
To solve the dilemma between the king and the one who dies as a redeemer, Messiah was given two roles:
The dilemma was solved by splitting the person of the Messiah in two: one of them, called Messiah ben Joseph (מָשִׁיחַ בֶּן־יוֹסֵף), was to raise the armies of Israel against their enemies, and, after many victories and miracles, would fall victim to Gog and Magog. The other, Messiah ben David (מָשִׁיחַ בֶּן־דָוִד), will come after him (in some legends will bring him back to life, which psychologically hints at the identity of the two), and will lead Israel to the ultimate victory, the triumph, and the Messianic era of bliss.1
This view is overly specific, since the prophet Isaiah adds a new implication to this theory of two separate messiahs: It becomes clear that he will bring himself back to life, and as such there are no two separate messiahs, but only one, who has two roles:
He saw that there was no man, and wondered that there was no one to intercede; then his own arm brought him salvation, and his righteousness upheld him.
Messiah would come as the son of Joseph, according to the Talmud, if people were not righteous. On the other hand, Messiah would come as the son of David, the righteous king, if they are:
Rabbi Alexandri said: Rabbi Joshua opposed two verses: it is written, And behold, one like the son of man came with the clouds of heaven (Dan. 7:13) while [elsewhere] it is written, [Behold, your king comes to you…] lowly, and riding on a donkey! (Zech. 9:7, in some translations v.9) — If they are righteous, [he will come] with the clouds of heaven; if not, lowly and riding on a donkey.
Babylonian Talmud, Seder Nezikin, Excerpt of Sanhedrin 98a4