There were no reports of injuries, three people were arrested and protesters did not clash with Trump supporters, who were seen leaving the rally.
It was unclear what sparked the sudden chaos, but some demonstrators were also hit with non-lethal rounds as authorities tried to break up the protest.
Some people had to clean out their eyes after the pepper spray was used.
During his speech, Trump lashed out at the press and hinted at pardoning former Sheriff Joe Arpaio over his recent conviction in federal court.
Trump blamed the media for the widespread condemnation he's received over his response to violence in Charlottesville, Virginia, that led to the death of a counter-protester. Trump said the press mischaracterized his response to the violence.
"To discover the source of division in our country, look no further than the fake news," he said. "These are very dishonest people and they're bad people. I don't think they like our country."
In addition to criticizing the media, Trump said Arpaio would be "just fine." The former Maricopa County sheriff is awaiting sentencing after his conviction in federal court for disobeying court orders to stop his immigration patrols.
There had been speculation that Trump was considering pardoning Arpaio, but he said at the rally that the only reason he wouldn't make a move from the stage was to avoid controversy for the moment.
Before the rally started, Trump supporters lined up for the event. Many of his supporters said they made the trip from Southern California.
"Honestly, I love the man so much that I would take a bullet for him because his life is more important to me than my own life," said Riverside resident Elizabeth McCright.
Placentia resident Mike Tucker said his mother is mad that he came, especially after the president's controversial comments about Charlottesville that suggested there were "good people" on both sides of the violent protests there.
"I don't have one racist bone in my body, and I don't believe the president of the United States does either," he said.
Phoenix police were at full staffing in an attempt to keep the peace between protesters and supporters during the rally.
Chief Jeri Williams said the department has been working 24-7 since Trump announced his visit.
Senators Jeff Flake, a vocal Trump critic, and John McCain, who recently voted against the Republican health care bill, did not join Trump for his rally.
One Trump supporter held up a sign saying McCain was a traitor.
"He committed treason against the country and against the GOP," she said.
Protesters said it's Trump who is disrespecting the Constitution.
"He's ignorant. He's a racist. His past shows racism. His daddy was a racist," Phoenix resident Andre Jenkins said.
When Trump first arrived in Arizona, he visited Yuma to meet troops and look at ways of combating illegal immigration before holding a rally in Phoenix later in the evening.
Trump stopped at a Marine Corps base that is a hub of operations for the U.S. Border Patrol. He looked at equipment used on the southern border, including a drone and boat.
After the tour, the president greeted service members in the 106-degree heat and signed "Make America Great Again" hats as well as took photos.
The Associated Press contributed to this report.