Andimba Toivo ya Toivo, who has died aged 92, led the struggle to liberate Namibia from apartheid South Africa as a freedom fighter in the mould of Nelson Mandela, with whom he was imprisoned on Robben Island.

Toivo’s uncompromising nature was exemplified when, in August 1967, he stood trial in a Pretoria court, charged with the capital offence of terrorism. “Is it surprising that in such times my countrymen have taken up arms?” he asked. “Violence is truly fearsome, but who would not defend his property and himself against a robber?” That robber was South Africa, which had been granted trusteeship of the German territory of Deutsch-Südwestafrika after the first world war, only to refuse to allow it independence after the second world war.

Judge Ludorf was known to be a hanging judge. He had done no military service, and as an advocate, he had defended a South African who had spied for the Germans. Toivo bravely pointed up the irony of the judge branding him a coward: “During the second world war, when it became evident that both my country and your country were threatened by the dark clouds of nazism, I risked my life to defend both of them … but some of your countrymen, when called to battle to defend civilisation, resorted to sabotage against their own fatherland.

“I volunteered to face German bullets, and as a guard of military installations, both in South West Africa and the republic, I was prepared to be the victim of their sabotage. Today, they are our masters and are considered the heroes, and I am called the coward.”

Toivo was sentenced to 20 years in jail, but his statement was widely read abroad and influenced international opinion. The trial led to a unanimous United Nations Security Council resolution in support of the UN General Assembly’s earlier revocation of South Africa’s mandate over the territory.

In prison Toivo was unbending, seizing every opportunity to show his disdain for his jailors. A fellow prisoner described the scene when Toiva responded to his treatment by a young warder: “Andimba unleashed a hard open-hand smack on the young warder’s cheek, sending [his] cap flying and [the warder] wailing (in Afrikaans), ‘The kaffir hit me’.” The inevitable spell of solitary confinement followed.

When Toivo was released in March 1984, short of his full term, he refused to leave his fellow prisoners and had to be coaxed out of his cell. He continued to fight for Nambia’s freedom in exile and, following independence in 1990, served as a government minister.

Toivo was born in Omangudu, a village in Ovamboland, northern Namibia, the son of Andimba Toivo ya Toivo and Nashikoto Elizabeth Malima. He went to a school run by Finnish Lutheran missionaries (toivo is the Finnish for hope), before training as a carpenter. In 1942, as pro-Nazi settlers were plotting to reconquer the colony, he volunteered for the South African Native Military Corps.

Toivo ya Toivo was a freedom fighter in the tradition of Nelson Mandela and they were both imprisoned on Robben Island. Photograph: Gallo Images

With the war over, he attended the Anglican St Mary’s mission school in Odibo, in order to learn English. After graduating as a teacher, he taught at St Mary’s before travelling to Cape Town in 1951 to broaden his horizons. There his day job was as a railway police officer, and he also mixed with leftwing students and trade unionists. He soon co-founded the Ovamboland People’s Organisation (OPO), which mobilised against South Africa’s continued occupation of Namibia. As a result of these activities, he was sent back to Namibia and found himself under house arrest in the northern village of Oniipa.

By now the Pretoria government had banned the African National Congress, and on 21 March 1960 shot peaceful protesters in the South African township of Sharpeville. A month later, OPO became the South West Africa People’s Organisation (Swapo), with the aim of attracting members beyond the Ovambo people. Toivo became its secretary general.

OPO had been non-violent, but Swapo set up a military wing, Plan (People’s Liberation Army of Namibia). When the international court of justice in the Hague decided, on a split vote, that it could not hear the case to decide on the legality of South Africa’s mandate in Namibia, Toivo admitted he had no answer to the question: “Where has your non-violence got us?”

In August 1966, a Swapo guerrilla camp in the north was attacked by South African soldiers. It was a rout. The captured men and their leaders were spirited away and held incommunicado in Pretoria while the Cape Town parliament rushed through the Terrorism Act, making it retrospective for four years. The day after the legislation was enacted, 30 Namibians were arraigned in court.

Professor John Dugard, a defence counsel in the trial, recalled that Toivo had been badly tortured. “But he grew in stature during the trial,” he said. “He was clearly the leader and respected as such. And this culminated in his amazing speech from the dock. He wrote it himself and counsel polished it.”

On his release, Toivo returned briefly to the Namibian capital, Windhoek, then joined Swapo exiles in Zambia. It was too late for any leadership ambitions he might have had and the organisation was by now controlled by Sam Nujoma, who would become Namibia’s president on independence in 1990. Toivo served in the new government, as minister of mines and energy, then minister of labour, and finally as minister of prisons and correctional services.

In 1990 Toivo married Vicki Erenstein, an American human rights lawyer whom he had met in New York a few months after his release from prison. They had twin daughters, and also raised two nephews. One, Isak, said: “He was simple, loyal, always grateful for everything, even for a plate of food we would give him, always positive and someone who never boasted.”

He is survived by Vicki and their daughters, Mutaleni and Nashikoto.

• Andimba Herman Toivo ya Toivo, freedom fighter and politician, born 22 August 1924; died 9 June 2017