Benjamin Mkapa, the third president of Tanzania and the leader of the country during a crucial period of democratic transition, died on Friday at a hospital in the port city of Dar es Salaam. He was 81.

His death was announced by President John Magufuli. Mr. Magufuli did not give a cause of death, but he declared seven days of mourning during which flags would fly at half-staff across the country.

“I will remember him for his great love for the nation, his piety, hard work and his efforts in building the economy,” Mr. Magufuli said in a message posted on Twitter. “Certainly, the nation has lost a strong pillar.”

Mr. Mkapa was the president of Tanzania from November 1995 to December 2005 and was the first leader elected after the return of multiparty politics in 1992. During his tenure, he played a central role in helping the country transition from a socialist system of development — popularly known as ujamaa — into a free-market economy.