The scheme would link Peru's Pacific coast with Brazil's Atlantic shores.
The decision came after talks between the Chinese Prime Minister Li Keqiang, and Peruvian President Ollanta Humala.
If completed, the railway would stretch 5,300km (3,300 miles) but campaigners fear the impact on indigenous people.
Brazil, China and Peru will now begin feasibility studies into the railway.
Mr Li secured Brazil's consent earlier this week, as part of his tour of Latin America.
The railway would "consolidate Peru's geopolitical position as a natural gateway to South America", President Humala said.
For China, it would reduce the cost of shipping raw materials and farm products.
But campaigners are concerned it might destroy untouched parts of the Amazon rainforest, affecting hundreds of indigenous communities.
Mr Li sought to ease fears, saying "to create the infrastructure, it is necessary to protect the environment" in a declaration with Mr Humala, AFP reported.
It is likely to cost more than $10 billion (Â£6.5 billion). The route is still being examined, but would begin in the gigantic Brazilian port of Acu and ending at a Peruvian port.
The Chinese President, Xi Jinping, pledged earlier this year to invest $250 billion (Â£161 billion) in Latin America over the next decade.

Based on the age of the surrounding rocks, this is the earliest known member of the clade that produced today's birds: Ornithuromorpha.
It pushes back the branching-out of this evolutionary group by at least five million years.
The little bird appears to have been a wader, capable of nimble flight.
The discovery is reported in the journal Nature Communications.
Birds began to evolve from the dinosaurs some 150 million years ago at the tail end of the Jurassic period. This is the age of the famous but hotly contested "first bird" Archaeopteryx - now considered by many to be a feathered dinosaur.
Some 20 million years later, when the newfound species was wading and flitting through what would become north-eastern China, palaeontologists believe there was quite a variety of bird life.
About half of those species were Enantiornithes, a group of early birds with teeth and clawed wings that eventually all died out.
The other half, including the new find, were Ornithuromorpha - a group that eventually gave rise to modern birds and looked much more like them.
The branching event behind that forked diversity is what the new discovery pushes back in time; previously the earliest known Ornithuromorph was 125 million years old.
The pair of skeletons that define the new species, christened Archaeornithura meemannae, were dug up from the Sichakou basin in Hebei province.
"The new fossil represents the oldest record of Ornithuropmorpha," said first author Wang Min, from the Chinese Academy of Sciences in Beijing. "It pushes back the origination date... by at least five million years."
The specimens were well preserved, revealing a number of details about A. meemannae. The bird stood about 15cm tall and its legs, even on the upper regions, had no feathers, which suggests a wading lifestyle.
The size and shape of its bones also suggest good manoeuvrability in the air.

The incident at Alliance Parade was reported shortly before 04:00 BST on Saturday.
Police said it is "believed that someone broke into the house and deliberately set it on fire".
They said there was no-one in the house at the time.
A Chinese scheme to build an east-west railway across South America, cutting across parts of the Amazon rain forest, has moved a step closer after Peru agreed to study the proposal.
[NEXT_CONCEPT]
Scientists in China have described a new species of early bird, from two fossils with intact plumage dating to 130 million years ago.
[NEXT_CONCEPT]
A house in north Belfast has been gutted in an arson attack.