The new prime minister is a career public servant who has worked with multiple governments. The replacement comes as Mr Macron is reshuffling the government to focus on restarting the French economy after months of lockdown due to coronavirus.
Mr Castex, 55, served for two years as the second-highest ranking official in the Elysee Palace during Nicolas Sarkozy’s presidency.
He was described by an Elysee official as a senior civil servant hailing from the centre-right of French politics whose experience in local politics will help Mr Macron connect with provincial France.
Reshuffles in France see the prime minister tendering his or her resignation ahead of cabinet appointments, but they can still be re-named to the position.
The move by Mr Macron to refashion his centrist government comes after voters punished him and his party in nationwide municipal elections, revealing a surge in support for the Green party.
Mr Philippe’s own victory in the northern port city of Le Havre has been described as a “rare bright spot” by local reports for the French president, as his party was expected to perform poorly there.
It was a political gamble for Mr Macron to replace the prime minister, who is more popular with the public than himself. Political analysts say Mr Philippe has shown steadfast loyalty during waves of unrest and could emerge as a presidential rival in 2022.
But keeping Mr Philippe in office would have been problematic for Mr Macron as it would have suggested his young party lacked the depth to allow for a complete cabinet overhaul.
In an interview given to several local newspapers, before Mr Philippe’s resignation, Mr Macron praised the now-former prime minister for his “outstanding work” in the past three years.
He said he is seeking a “new path” to rebuild France for the two remaining years of his term.
“The return from summer holidays will be difficult, we must get ready,” he was quoted as saying in regional newspapers.
Mr Philippe said during his campaigning in Le Havre: “If he the President thinks someone else would be more useful as prime minister, I will in complete loyalty respect his choice.”