Yes, it will.
CAN has almost the same architecture as Ethernet. CAN IDs play almost the same role as MAC addresses in Ethernet. So as long as the two network segments remain separate, you can have same ID on two different devices. If these segments join back into one network - you will have collisions and both devices become unusable for proper communication.
I am not sure what do you mean by "same master in both networks", since CAN is designed to be closer to p2p protocols and does not really have master-slave relationship between hosts.
If you mean to keep message priority for 'masters' after joining network segments - you can just make a gap in the IDs and assign both 'masters' something small, while 'slaves' would have IDs higher than both masters.
Edit: I never worked (or saw) such multi-port CAN devices. But from a pure theoretical point of view - as long as device itself allows same IDs on its ports - it will probably work. As long as ID is unique inside the bus - the host will be able to communicate.
There are 'special case' devices which are 'CAN gateway'. They join two CAN buses into one. But the gateway is usually not referred as 'master', so I am not sure this is what you have in mind.
Once you connect to buses with a gateway - you have just one bus and all devices from both segments can communicate with each other. The gateway is just a signal repeater. BTW if your bus is physically long enough - you can intentionally put a gateway in the middle to ensure signal strength.
CAN-CAN gateways are configured by a specialized software, which is distributed together with the gateway device (or downloaded from the production company). So the question of IDs on its ports is answered automatically, when you run the tool :)
Another multi-port devices I worked with is a CAN-Ethernet gateway. But in this case, the device has just one CAN port and it acts as a normal CAN device with a normal ID (and normal MAC address on the Ethernet side).