Hiroo Onoda, an Imperial Japanese Army officer who remained at his jungle post on an island in the Philippines for 29 years, refusing to believe that World War II was over, and returned to a hero’s welcome in the all but unrecognizable Japan of 1974, died Thursday in Tokyo. He was 91.

His death, at a hospital there, was announced by the Japanese government.

Onoda, a second lieutenant, was one of the war’s last holdouts: a soldier who believed that the emperor was a deity and the war a sacred mission; who survived on bananas and coconuts and sometimes killed villagers he assumed were enemies; who finally went home to the lotus-land of paper and wood that turned out to be a futuristic world of skyscrapers, television, jet planes, pollution and atomic destruction.

Onoda, a small, wiry man of dignified manner and military bearing, seemed to many like a samurai of old, offering his sword as a gesture of surrender to President Ferdinand E. Marcos of the Philippines, who returned it to him.

And his homecoming, with roaring crowds, celebratory parades and speeches by public officials, stirred his nation with a pride that many Japanese had found lacking in the postwar years of rising prosperity and materialism. His ordeal of deprivation may have seemed a waste to much of the world, but in Japan it was a moving reminder of the redemptive qualities of duty and perseverance.

It happened with a simple command: Onoda’s last order in early 1945 was to stay and fight. Loyal to a military code that taught that death was preferable to surrender, he remained behind on Lubang Island, 93 miles southwest of Manila, when Japanese forces withdrew in the face of a U.S. invasion.

After Japan surrendered, that September, Onoda, an intelligence officer trained in guerrilla tactics, and three enlisted men with him found leaflets proclaiming the war’s end but believed they were propaganda tricks. They evaded U.S. and Filipino search parties and attacked islanders they took to be enemy guerrillas; about 30 inhabitants were killed in skirmishes with the Japanese over the years. One of the enlisted men surrendered to Filipino forces in 1950, and two others were shot dead, one in 1954 and another in 1972, by island police officers.

The last holdout, Onoda — officially declared dead in 1959 — was found by Norio Suzuki, a student searching for him in 1974. The lieutenant rejected Suzuki’s pleas to go home, insisting he was still awaiting orders. Suzuki returned with photographs, and the Japanese government sent a delegation, including the lieutenant’s brother and his former commander, to formally relieve him of duty.

In Manila, the lieutenant, wearing his tattered uniform, presented his sword to Marcos, who pardoned him for crimes committed while he thought he was at war.

He was already a national hero when he arrived in Tokyo, met by his aging parents and huge flag-waving crowds. More than patriotism or admiration for his grit, his jungle saga, which had dominated the news in Japan for days, evoked waves of nostalgia and melancholy.

The 52-year-old soldier spoke earnestly of duty and seemed to personify a devotion to traditional values that many Japanese thought had been lost.

“I was fortunate that I could devote myself to my duty in my young and vigorous years,” he said.

Asked what had been on his mind all those years in the jungle, he said: “Nothing but accomplishing my duty.”

After his national welcome in Japan, Onoda was given a military pension and signed a $160,000 contract for a ghostwritten memoir, “No Surrender: My Thirty Year War.” In 1975, he moved to a Japanese colony in São Paulo, Brazil, where he raised cattle. In recent years, he lived in Japan and in Brazil, where he was made an honorary citizen.