Jewish tradition speaks of two redeemers, each one called Mashiach. Both are involved in ushering in the Messianic era. They are Mashiach ben David and Mashiach ben Yossef.
The term Mashiach unqualified always refers to Mashiach ben David (Mashiach the descendant of David) of the tribe of Judah. He is the actual (final) redeemer who shall rule in the Messianic age. All that was said in our text relates to him.
Mashiach ben Yossef (Mashiach the descendant of Joseph) of the tribe of Ephraim (son of Joseph), is also referred to as Mashiach ben Ephrayim, Mashiach the descendant of Ephraim. He will come first, before the final redeemer, and later will serve as his viceroy.
The essential task of Mashiach ben Yossef is to act as precursor to Mashiach ben David: he will prepare the world for the coming of the final redeemer. Different sources attribute to him different functions, some even charging him with tasks traditionally associated with Mashiach ben David (such as the ingathering of the exiles, the rebuilding of the Bet Hamikdash, and so forth).
The principal and final function ascribed to Mashiach ben Yossef is of political and military nature. He shall wage war against the forces of evil that oppress Israel. More specifically, he will do battle against Edom, the descendants of Esau. Edom is the comprehensive designation of the enemies of Israel, and it will be crushed through the progeny of Joseph. Thus it was prophesied of old, "The House of Jacob will be a fire and the House of Joseph a flame, and the House of Esau for stubble.." (Obadiah 1:18): "the progeny of Esau shall be delivered only into the hands of the progeny of Joseph."
This ultimate confrontation between Joseph and Esau is alluded already in the very birth of Joseph when his mother Rachel exclaimed, "G‑d has taken away my disgrace" (Genesis 30:23): with prophetic vision she foresaw that an "anointed savior" will descend from Joseph and that he will remove the disgrace of Israel. In this context she called his name "Yossef, saying 'yossef Hashem - may G‑d add to me ben acher (lit., another son), i.e., ben acharono shel olam - one who will be at the end of the world's time,' from which it follows that 'meshu'ach milchamah - one anointed for battle' will descend from Joseph."
The immediate results of this war will be disastrous: Mashiach ben Yossef will be killed. This is described in the prophecy of Zechariah, who says of this tragedy that "they shall mourn him as one mourns for an only child." (Zechariah 12:10). His death will be followed by a period of great calamities. These new tribulations shall be the final test for Israel, and shortly thereafter Mashiach ben David shall come, avenge his death, resurrect him, and inaugurate the Messianic era of everlasting peace and bliss.
This, in brief, is the general perception of the "second Mashiach," the descendant of Joseph through the tribe of Ephraim.
Quite significantly, R. Saadiah Gaon (one of the few to elaborate on the role of Mashiach ben Yossef) notes that this sequence is not definite but contingent! Mashiach ben Yossef will not have to appear before Mashiach ben David, nor will the activities attributed to him or his death have to occur. All depends on the spiritual condition of the Jewish people at the time the redemption is to take place:
The essential function of Mashiach ben Yossef is to prepare Israel for the final redemption, to put them into the proper condition in order to clear the way for Mashiach ben David to come. Of that ultimate redemption it is said, that if Israel repent (return to G‑d) they shall be redeemed immediately (even before the predetermined date for Mashiach's coming). If they will not repent and thus become dependent on the final date, "the Holy One, blessed be He, will set up a ruler over them, whose decrees shall be as cruel as Haman's, thus causing Israel to repent, and thereby bringing them back to the right path." In other words, if Israel shall return to G‑d on their own and make themselves worthy of the redemption, there is no need for the trials and tribulations associated with the above account of events related to Mashiach ben Yossef. Mashiach ben David will come directly and redeem us.
Moreover, even if there be a need for the earlier appearance of Mashiach ben Yossef, the consequences need not be as severe as described. Our present prayers and meritorious actions can mitigate these. R. Isaac Luria (Ari-zal) notes that the descendant of Joseph, by being the precursor of the ultimate Mashiach, is in effect kissey David, the "seat" or "throne" of David, i.e., of Mashiach. Thus when praying in the daily Amidah, "speedily establish the throne of Your servant David," one should consider that this refers to Mashiach ben Yossef and beseech G‑d that he should not die in the Messianic struggle. As all prayers, this one, too, will have its effect.
It follows, then, that all the above is not an essential or unavoidable part of the Messianic redemption that we await. Indeed, it - (and the same may be said of the climactic war of Gog and Magog) - may occur (or may have occured already!) in modified fashion. This may explain why Rambam does not mention anything about Mashiach ben Yossef. R. Saadiah Gaon and R. Hai Gaon, as well as a good number of commentators, do refer to him briefly or at length. In view of the divergent Midrashim and interpretations on this subject it is practically impossible to present a more definitive synopsis that would go far beyond the above. Thus it is wisest to cite and follow R. Chasdai Crescas who states that "no certain knowledge can be derived from the interpretations of the prophecies about Mashiach ben Yossef, nor from the statements about him by some of the Geonim;" there is no point, therefore, in elaborating on the subject.