He also helped Ivory Coast qualify for the 2010 World Cup and Japan for the 2018 edition in Russia but on both occasions he was fired before the tournament.
Halilhodzic will receive a monthly salary of 80,000 euros ($89,000), Moroccan soccer federation president Fouzi Lekjaa said in a news conference.
But the new coach has been tasked with reaching the semi-finals of the next Africa Cup of Nations in 2021, qualifying Morocco for the Qatar World Cup the year after and winning the Cup of Nations in 2023.
"Failure to achieve any of these targets will automatically result in termination of the contract," said Lekjaa.
"It is a big responsibility to coach Morocco, a challenge and an honour,” the new coach told reporters. (Editing by Pritha Sarkar)