not knowing what interfaces you are working with, the simple answer would be to have the incoming call routed to an analogue station interface (ONS or FXS) and then have that line connected to the input of your fax/modem.
If you are on digital or SIP you can route based on DID/DDI
If you are trying to share an analogue line then you could do any of the following (at least):
1) route based on distinctive ring
2) twin the call to ring your internal number and also your analogue port and have the fax pick up based on distinctive ring
3) set the faxmodem to pick up after say 4 rings.
4) If you have some form of vmail or auto attendant then you could forward based on Fax 2200Hz tone detection.
I use commercial products primarily which can do all of the above. For asterisk I think it can do option (2) by twinning (aka dual forking) as long as it also transport the ring pattern.
Worse comes to worse you can do a variant of (2) where you bridge tap ahead of the analogue port going into the asterisk Media gateway and faxmodem in parallel. the only issue of course is if the faxmodem picks it up, asterisks can't track the call as it could only see it as an abandoned incoming call. This could lead to side effects which you may or may not care about such as thinking the line is free, no BLF, etc...