The trip was a Polish idea, after the EU warned of potential security risks.
The leaders decided to go by train because flying by Polish military jet could have been viewed by Russia as dangerously provocative, BBC Europe editor Katya Adler reported. It was not immediately clear when their train would make the return trip to Warsaw.
Poland’s Mateusz Morawiecki said history was being made in Ukraine’s capital.
“It is here, that freedom fights against the world of tyranny. It is here that the future of us all hangs in the balance,” he tweeted. Mr Morawiecki added that Ukraine could count on the help of its friends.
The prime ministers sat down for a briefing with their Ukrainian counterpart Denis Shmyhal, and President Volodymyr Zelensky, who thanked them for the “powerful” gesture of support.
They were accompanied to Ukraine by Jaroslaw Kaczynski, the leader of Poland’s ruling Law and Justice party.
Sepsis and hunger as civilians hide
Russian artillery and warplanes are continuing to pound cities and towns across Ukraine.
In Mariupol, a key port city in the south-east, hundreds of people crammed into the basement of a large public building are running out of food, with many also in need of urgent medical help, the news Hugo Bachega was told.
“Some have developed sepsis from shrapnel in the body,” said Anastasiya Ponomareva, a 39-year-old teacher who fled the city at the start of the war but is in contact with friends there. “Things are very serious.”
Her friends are with other families who spend most of their day in the basement. From time to time they go upstairs for sunlight, but rarely outside. They have all left homes that are no longer safe or no longer standing.
At an intensive care hospital on the western outskirts of the city, staff described being treated like hostages by Russian forces.
One employee was quoted as saying that Russian troops had “forced 400 people from neighbouring houses to come to our hospital,” adding: “We can’t leave.”
The regional governor, Pavlo Kyrylenko, said the facility had been all but destroyed by shelling in recent days, but that staff had continued to treat patients in the basement.
Separately, about 2,000 cars were able to leave Mariupol along a humanitarian evacuation route, according to city authorities. Before the war around 400,000 people lived in the city, which has endured intense bombardment by Russian forces. The city council says well over 2,000 civilians have died.
Cameraman and journalist killed in Kyiv
A cameraman and a journalist working for Fox News were killed when their vehicle was struck by incoming fire on the outskirts of the Ukrainian capital Kyiv, staff at the US network said.