I believe the ship would technically be moving -therefore underway- so the national ensign would be flown from a mast. If the ship you are representing-say the USS Texas- were making a transit before we gave the canal to Pananama no Panamanian flag would be flying since the canal zone was US territory. After the canal was given to Panama the Panamanian flag would be flown.
But that's not the end of it. If an admiral was embarked then his pennant would be flown. Also probably a four flag hoist port and starboard of the ship's radio call sign. Ours was N-P-R-X. November,Papa,Romeo,Xray.
Another interesting item. It appears that battleships and other ships with aircraft cranes on the stern had no place to fly the ensign while moored. Therefore it was always flown from a mast. The jack would be in the normal place when moored.