Sarah Huckabee Sanders, the deputy press secretary, will replace Spicer, the White House announced on Friday.
Spicer told the Associated Press news agency that he was stepping down to give Scaramucci a fresh start.
He said he felt it would be best for Scaramucci to be able to build his own operation “and chart a new way forward”. The White House was at a point where Trump “could benefit from a clean slate”, he added.
In a Twitter post, Spicer said he would continue his service through August.
— Kayleigh McEnany (@PressSec) July 21, 2017
In a statement read by Sanders, Trump said he was grateful for Spicer’s work.
“Just look at his great television ratings,” he added.
Scaramucci, who also addressed reporters at the White House, called Spicer a “true American patriot” and said he hoped Spicer “goes on to make a tremendous amount of money”.
Spicer’s departure reflected turmoil within Trump’s legal and communication teams amid a widening investigation into possible ties between Trump’s 2016 presidential campaign and Russia.
James Bays, Al Jazeera’s Washington editor, said Spicer had been “sidelined in recent weeks”.
“We understand that he’s unhappy with the choice for the White House communications director, and that is why he is now quitting his job,” Bays said.
Parodied for his combative encounters with the White House press corps, Spicer became one of the Trump administration’s most recognised figures after taking the job in December.
He was sometimes targeted by critics for what they said were false or misleading statements.
Spicer spent several years leading communications at the Republican National Committee before helping Trump’s campaign in the general election.
Mark Corallo, who coordinated the Trump legal team’s public response to the Russia probe, also stepped down, according to an email he sent to the AFP news agency.
No reason was given for Corallo’s departure.