Contract trains and run-through power.
Contract trains are where some trains originate at a certain point, and terminate on a certain point, but need to operate over more than one railroad.
For example, the RailEx produce train originates in Seattle, WA and is hauled by Union Pacific. Its destination is the RailEx facility at Schenectady, NY, which is on CSX. The train is contracted to run express as a unit train so Union Pacific provides the engines. When the train is interchanged to CSX in Chicago, CSX picks up the train, engines and all and brings the train to Schenectady.
Another example would be Norfolk Southern train 22K. It originates in Chicago, IL, and terminates in Ayer, MA. It operates from Chicago, IL to Glenville, NY under Norfolk Southern. It is a Norfolk Southern train that needs to operate over multiple railroads to get to its destination. At Mohawk Yard in Glenville, the train is split, with one section going to the Delaware and Hudson (Canadian Pacific) and the other section for Ayer, MA. The Ayer, MA section is taken by the D&H to Mechanicville, NY where Pan Am Railways picks it up with the NS provided engines still on the train. (It doesn't always run with NS engines, it runs with whatever NS puts on the train in Chicago. I'll explain more in the second part). The train is still an NS train, but is now operated by a Springfield Terminal (Pan Am Railways) crew.
Trains are operated this way because when the train is interchanged, all that needs to be done is a quick refuel and crew change, and the train is on the way. There would be no need to swap engines which would take extra time and tie up resources (the host railroad may or may not be able to spare the engines needed to run the train)
Another way that foreign engines can enter another railroad is similar to the above, but the contract to keep the engines on the train is different. The railroads operate on a horsepower hours agreement. For example, CSX's train Q426 and Q427 run from Selkirk, NY to Portland, ME. However, to get to Portland, ME, the train has to travel over Pan Am Railways. CSX provides the engines from Selkirk, and the train is interchanged at Worcester, MA. Pan Am takes the train, engines and all to Ayer, MA where they cut off certain cars, then takes the train to Portland, ME. The agreement is where Pan Am would owe CSX horsepower hours for using the engines on Pan Am's railroad. Pan Am would have to send a locomotive or two to work on CSX to repay the horsepower hours.
While horsepower hours are being exchanged, the engines can be sent to other railroads, or work whatever job the railroad wants.
For example, Pan Am had a grain train last year where the engines were provided by Union Pacfic, but the engines were Ferromex engines repaying horsepower hours to UP.
When Pan Am sends CSX GP40s to repay horsepower hours, they get put into local service (and take forever to repay the hours since they would be used infrequently). When Pan Am sends SD40-2s, they can pay off horsepower hours faster as CSX would have a better use for them in road freights.
While the contracts can differ, most contracts dictate that while the engine is on a foreign railroad, the foreign railroad is responsible for all maintenance and fuel.
A few years ago, Pan Am was paying back horsepower hours to CSX with a GP40. It was wrecked while on CSX, so CSX had to send them a new (used) GP40. This GP40 was ex-LN (I think) and is the only standard GP40 (now numbered MEC 381) on Pan Am with nose headlights. Same thing happened on NS. NS wrecked MEC 375, a high hood GP40, and NS replaced it with another high hood GP40 (now MEC 382). It is now the only GP40 on Pan Am with NS numberboards, and high mounted flashing front and rear ditch lights.