Put the rack next to the frame, then the fender, then use a stainless steel M5 allen bolt with a washer.
If you put the fender in forst, the rack will have more leverage on the bolt and may snap it.
Make sure the bolt is long enough to protrude a few threads but not interfere with the chain. Carry some spare bolts. I replace short waterbottle bolts with one long enough for the rack, you can ride without a waterbottle in an emergency.