How does a router know that it should receive the packet, if the packet was sent to a public IP address?

A network with Internet and Local is setup as follows.

Your network is connected to the internet through a Modem. A modem can exchange information from and to the internet to exactly 1 device.

You usually have more than one devices in your network. In order to allow these devices to communicate with each other, you need something with multiple network ports and a bit of software to manage this. In the past, this was a network hub. A hub basically copies incoming traffic to all ports, and whoever listens will respond.

Research allowed these devices to become smart and that's how the switch was created. A switch is slightly different than a Hub. It will try to figure out who the destination is by transmitting a new connection to all ports and seeing who replies. When it gets a reply on a port, it will tag that port with the ipaddress that was given in the initial request and will route all traffic for that ip address to that port without broadcasting it to all the other ports.

The modem and the switch cannot be used together to connect local pc's to the internet, so a new device was created, called a Router. A router sits in between the modem and the switch. A router is usually packed with extra functionality, such as a DHCP server, allowing the management of a local network. The router will have a lookup table that will help figuring out if an ipaddress is meant to be local or on the internet. It will also have a table with port mappings so you can open a port from the internet and forward its traffic to a local device.

So. Lets say, PC1 with IP: 192.168.1.2 sents a packet out to 192.168.1.3. The packet is transmitted out of the networkcard over its attached cable. The packets reach the port on the switch.

The switch has 3 ports enabled: Port WAN, which goes to the router, Port 1 which is 192.168.1.2 (itself) and Port 2 which is 192.168.1.3. The router will transmit it to the WAN port and port 2 unless it already figured out that 192.168.1.3 belongs to and sends the packet directly to that port.

Lets say your computer now transmits a packet to 11.22.33.44. The packet travels over the network card and reaches the switch again. It has no collection of this ip address, so it sends it to all ports. The router receives the packet and forwards it to the modem, which transmits it to the internet.

A bit later, the internet responds and a package comes in from the modem. This is being forwarded to the router who recognizes this is a response to the earlier packet (through uPNP) and forwards it to the switch. The switch forwards it back to your port.

This is how a router works in a nutshell.

I hope it made sense. :)

Also, in case your question is that 2 devices has the same ip, you cannot have 2 devices with the same ip address. You get an ip conflict and the network will not work correctly.