Volodymyr Zelensky inspected damaged buildings and met soldiers, officials and health workers, his office said.
Both cities are key targets for Russian forces trying to secure the Black Sea coast.
For weeks, the Russian offensive has been focused on seizing the Donbas region in eastern of Ukraine.
The Ukrainian president has met several Western leaders who have travelled to the capital, Kyiv, almost regularly.
But he has also defied Russian troops by going to areas where fighting has been raging.
Western sanctions ‘mad and thoughtless’ – Putin
Russians dying for Putin’s ambitions – Klitschko
During his last trip, Mr Zelensky told Ukrainian troops on the front line, “Take care of Ukraine – the only thing we have”, his office said.
Russia invaded Ukraine on 24 February, saying it aimed to “de-Nazify” and “demilitarise” its former Soviet neighbour.
In the ferocious war which has followed, the UN says that more than 4,500 civilians have been killed and nearly 5,600 have been injured. Thousands of combatants have also been killed or injured on both sides.
More than 13 million people have fled their homes, according to the UN.
The West has responded to the invasion by imposing severe sanctions on Moscow and supplying weapons to Ukraine but has not intervened directly in the war against Russia, a nuclear superpower.
The UK’s Ministry of Defence said on Saturday that Russia was likely to be renewing its efforts to advance into the Donetsk region -which alongside the Luhansk makes up the Donbas.
The Ukrainian military said it had “repelled Russian attacks” near Krasnopillya, in Donetsk, and Syrotyne, in Luhansk, with operations continuing around Severodonetsk.
Meanwhile, the secretary of Ukraine’s National Security Council has dismissed reports from certain government officials that as many as 500 Ukrainian soldiers could be dying every day.
Oleksiy Danilov said the officials didn’t have access to the relevant information. Previously Ukrainian officials have spoken of up to 100 deaths a day.
In Kyiv, Mayor Vitali Klitschko has told the BBC his country will only enter peace talks after the “last Russian soldier has left Ukraine”.
The former boxer said the Russian people would eventually realise their own soldiers were dying for nothing more than President Vladimir Putin’s ambition.
After returning from his trip to Kyiv on Friday, the UK Prime Minister Boris Johnson warned against “Ukraine fatigue” setting in, saying it was important to show that allies were standing with Ukraine for the long haul in its war against Russia.
He said it would be a “catastrophe” if Mr Putin was able to secure territorial gains already made.