Sip phone checking

Hi.
Could you explain me the following:
We have Asterisk as a server and two sip phones A and B as clients behind NAT, so they have “host=dynamic” and “nat=yes”.
So, when phone A dial to B (user just call B phone number) via Asterisk - how Asterisk know what IP address must be used to reach phone B???

Thank you.