The 51-year-old son of peasant farmers was sworn in inside the Peruvian Congress.
"I swear by God, by my family, by the peasants, by the Indigenous peoples, by the ronderos [peasant patrols], fishers, professionals, children, adolescents, that I will exercise the office of President of the Republic," Castillo said.
"I swear by the people of Peru for a country without corruption and for a new constitution."
Castillo won the June 6 election with a slim majority of just 44,000 votes.
The election of Castillo has shaken up the Peruvian political establishment. The Andean country's large copper mining interests — Peru is the world's second-largest producer of the metal — are fearful of plans to increase taxes to pay for welfare reforms.
Peru celebrates 200 years of independence
The inauguration coincides with Peru's 200-year anniversary of independence from Spain. King Felipe VI of Spain, along with the presidents of Argentina, Chile, Bolivia and Ecuador, were in Lima to mark the occasion.
Castillo became a recognizable image thanks to his trademark white, wide-brimmed hat, traditional in his region of Cajamarca, where the last Inca emperor, Atahualpa, was executed in 1533.
He branded himself as a "man of work, a man of faith, a man of hope" and called for "no more poor people in a rich country." He also had pledged to nationalize Peru's rich mining and hydrocarbon industries, but has since softened his tone.
He is socially conservative, opposing gay marriage, abortion and euthanasia. He has also proposed withdrawing from the American Convention on Human Rights so that Peru can reintroduce the death penalty.
His Free Peru party supports the regime of Nicolas Maduro in Venezuela, unlike many other left-wing parties in Peru.
What are the challenges facing Castillo?
Castillo's electoral win was only finally confirmed last week. His rival in the final round of voting was right-wing candidate Keiko Fujimori, daughter of jailed former president Alberto Fujimori.
Keiko is also facing corruption charges that could land her in jail. She contested the results of the election, claiming fraud on Castillo's part.
Peru: Castillo wins presidency
The new president will have to deal with a fragmented Congress where he will likely have a difficult time passing some of his more radical reforms, including plans to rewrite the constitution.
He will also have to deal with the far-left wing of his Free Peru party, led by Marxist neurosurgeon Vladimir Cerron.
"Castillo needs to unite the hard core of his party, but he has to do it without destroying the image the people have of him, which is that he is against radicalism," Jeffrey Radzinsky, a Lima-based governance expert, told Reuters.