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.
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.