I'm curious how will Imam Mehdi die as he will probably be martyred like every other Imam but if he dies, won't that mean the Earth would be left without a hujjah?

There is some contention surrounding this subject. We do know that the Imam (as) will die, as every soul shall taste death.
We believe in الرجعة (the return). That the Imams will return, a narration says that he will be buried by imam Husayn (as). So therefore the will still be hujjah until kingdom come.