Initially, Gandhi was simply affronted that discriminatory laws and bigoted custom lumped educated well-to-do Indians like him with “coolies,” the impoverished mine, plantation and railroad workers who made up the bulk of the region’s immigrant Indian population. The nonviolent campaigns he waged to bring about equality between Indians and whites over the next 20 years would lead him — slowly and unsteadily, but inexorably — to advocate equality between Indian and Indian, first across caste and religious lines and then between rich and poor. (His identification with the aspirations of black people would not come until long after he had left Africa.)

As Lelyveld shows, the outcomes of Gandhi’s campaigns in South Africa were neither clear-cut nor long-lasting: after one, his own supporters beat him bloody because they thought he’d settled too quickly for a compromise with the government. But they taught him how to move the masses — not only middle-class Hindu and Muslim immigrants but the poorest of the poor as well. He had, as he himself said, found his “vocation in life.”

Soon after returning to India in 1915, Gandhi set forth what he called the “four pillars on which the structure of swaraj” — self-rule — “would ever rest”: an unshakable alliance between Hindus and Muslims; universal acceptance of the doctrine of nonviolence, as tenet, not tactic; the transformation of India’s approximately 650,000 villages by spinning and other self-sustaining handicrafts; and an end to the evil concept of untouchability. Lelyveld shrewdly examines Gandhi’s noble but doomed battles to achieve them all.

He made a host of enemies along the way — orthodox Hindus who believed him overly sympathetic to Muslims, Muslims who saw his calls for religious unity as part of a Hindu plot, Britons who thought him a charlatan, radical revolutionaries who believed him a reactionary. But no antagonist was more implacable than Bhimrao Ramji Ambedkar, the brilliant, quick-tempered untouchable leader — still largely unknown in the West — who saw the Mahatma’s nonviolent efforts to eradicate untouchability as a sideshow at best. He even objected to the word ­Gandhi coined for his people — “Harijans” or “children of God” — as patronizing; he preferred “Dalits,” from the Sanskrit for “crushed,” “broken.”

Sometimes, Gandhi said Indian freedom would never come until untouchability was expunged; sometimes he argued that untouchability could be eliminated only after independence was won. He was unapologetic about that kind of inconsistency. “I can’t devote myself entirely to untouchability and say, ‘Neglect Hindu-Muslim unity or swaraj,’ ” he told a friend. “All these things run into one another and are interdependent. You will find at one time in my life an emphasis on one thing, at another time on [an]other. But that is just like a pianist, now emphasizing one note and now [an]other.” It was also like the politician he said he was, always careful to balance the demands of one group of constituents against those of another.