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.1
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.2 He will come first, before the final redeemer, and later will serve as his viceroy.3
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).4
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.5 Edom is the comprehensive designation of the enemies of Israel,6 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."7
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.8 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,'9 from which it follows that 'meshu'ach milchamah - one anointed for battle' will descend from Joseph."10
The immediate results of this war11 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).12 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.13
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."14 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.15