You should first understand how ip address allocated?
Also you need to understand how dhcp and nat work?
When you start r&d on this protocol , you figure our concept called private network .
In this private network , device have dummy ip address ( whatever is ) but have unique in that private network.
Whenever you device send or receiver response it used router ip address because it unique in internet.
Router maintain table and record following things :-
Sender ip address
Sender port address
receiver ip address
receiver port address.
Now router modify only above two field of router by own data.
When data come from receiver side and it again check which device for this data come. It check table and match forward response that device and that service.