The royal couple were not hit by the three eggs, which were thrown as they walked along the Micklegate Bar area of the city.
There was a large crowd in the city for the royal visit.
A man was heard shouting “this country was built on the blood of slaves” as he was detained by about four police officers. Other people in the crowd started shouting “God save the king” and “shame on you” at the man.
People in the crowd restrained the egg thrower before police detained him.
North Yorkshire Police confirmed that a 23-year-old man had been arrested on suspicion of a public order offence following an incident on Micklegate.
He remains in police custody, a police representative said.
The king and queen consort are on a two-day visit to Yorkshire to carry out a number of engagements, including the unveiling in York of a statue of Queen Elizabeth II, the first to be installed since her death.
Later, they attended a ceremony to officially confer city status on Doncaster.
As police detained the man, the king continued with the traditional ceremony in which the sovereign is officially welcomed to the city of York by the Lord Mayor. It was last carried out when his mother visited the city in 2012.
The king appeared unfazed after the incident as he greeted some members of the crowd.
King Charles said earlier this week that he believes Britain’s role in the slave trade should not be hidden. The king was speaking in Leeds while visiting the World Reimagined Globes project, which explains Britain’s role in slavery.