The planned rail test, set for Thursday, would be the first time trains have crossed the tightly sealed border since inter-Korean rail links were severed in the middle of the 1950-53 Korean War.
Two tracks have been reconnected as part of a series of reconciliation projects launched since the two sides held the first-ever summit of their leaders in 2000.
The test would be just a single run of trains along two restored tracks on each side of the peninsula, with regular train service between the two sides unlikely to begin anytime soon.
The two Koreas had agreed in principle on security for the rail test earlier this week, but the adoption of a formal agreement came only after more than 30 hours of extended talks that stretched into Friday due to unspecified sticking points.
South Korea hopes the inter-Korean railways could ultimately be linked to Russia's Trans-Siberian railroad and allow an overland route connecting the peninsula to Europe — significantly cutting delivery times for freight that now requires sea transport.
Economic officials from both sides agreed last month to conduct the train run, but North Korea's military had the final say on whether it goes forward because such a border crossing requires security arrangements.
This week's talks were the first high-level military contacts between the two sides in a year. The two Koreas remain technically at war because the Korean War ended in a cease-fire that has never been replaced with a peace treaty.
Ties between the two sides have warmed significantly since the 2000 summit, although they suffered during the international standoff over North Korea's nuclear ambitions.
Pyongyang conducted its first-ever nuclear test in October, chilling relations with the South. But the South began reaching out again to the North after Pyongyang agreed in February to shut down its nuclear reactor under an agreement with the United States and four other neighbors.
Still, the communist regime missed an April deadline to close the reactor because of a separate financial dispute with the United States, and it is unclear when it will close down the facility.