It looked like France might make short work of their opponents when Hernandez scored inside five minutes at Al Bayt Stadium,
despite having a hostile crowd against them.
But Morocco, the first African and Arab team ever to reach the last four at a World Cup, fought on despite losing key players to injury only to see substitute Kolo Muani seal victory for Les Bleus with a late second.
It will be France's fourth World Cup final in seven editions and they are hoping to become the first team since Brazil 60 years ago to retain the trophy when they take on Argentina at Lusail Stadium on Sunday.
That will be billed as a showdown between Messi and his Paris Saint-Germain teammate Kylian Mbappe, but this match was not about the latter's individual brilliance.