Ford has a technical service bulletin out for this concern. Here is the TSB. The problem more than likely is the transfer case shift motor, but there is a possibility it could be the GEM module. From the way I read this tsb you replace the shift motor (which is on the rear of the transfer case) and if that doesn't do it you replace the GEM module.
ELECTRICAL - ELECTRONIC SHIFT ON THE FLY(ESOF) TRANSFER CASE - INADVERTENT 4X4 HIOR LOW SHIFT EVENTS
Some vehicles may exhibit inadvertent 4X4 Hi or Low shift events, 4X4 and/or Low Range indicator flashing or solid or vehicle stuck in 4L after uncommanded shift. This may be caused by the 4X4 shift motor.
Install a YL1Z-7G360-AA shift motor on vehicles built before 12/99. The YL1Z-7G360-AA shift motor with a grey contact plate cover replaces the prior design F75Z-7G360-AA motor with a blue contact plate cover. Grey cover shift motors contain improved sense plate material and shift motor terminal upgrade. For vehicles already equipped with the latest style shift motor, refer to the following Service Procedure for details.
For overall description and operation of the Electronic Shift On The Fly (ESOF) Transfer Case refer to Section 308-07 of the appropriate model year Workshop Manual.
NOTE ALL DIAGNOSIS BY SYMPTOM SECTIONS HEREIN ARE WRITTEN ASSUMING THAT VEHICLE IS ALREADY EQUIPPED WITH THE LATEST STYLE SHIFT MOTOR. ALWAYS REPLACE SHIFT MOTOR AS A FIRST STEP IF CURRENT MOTOR IS PRIOR LEVEL WITH BLUE COVER.
NOTE TO RESET THE GEM'S 4X4 MONITORING SOFTWARE, USE CLEAR DTC COMMAND IN THE NGS OR WOS TESTER (EVEN IF NO DTC'S ARE AVAILABLE) AND CYCLE IGNITION KEY ON-OFF-ON. RECORD ANY CONTINUOUS MEMORY CODES PRIOR TO CLEARING DTCs.
NOTE AN UNCOMMANDED 4H OR 4L SHIFT DUE TO FALSE SHIFT MOTOR CONTACT PLATE SIGNAL MAY OR MAY NOT BE ACCOMPANIED BY SHIFT MOTOR DTC'S P1838, P1846, P1850, P1854, P1858, P1866, AND/OR P1867 STORED IN THE GEM DEPENDING ON WHETHER THE IGNITION KEY WAS CYCLED AFTER THE EVENT.
NOTE CONTACT PLATE PIDS READ OPPOSITE OF 1999 OR EARLIER WORKSHOP MANUALS WHEN VIEWED WITH NGS RED CARD VERSIONS 15.0 OR LATER. THIS TSB CONTENT REFLECTS USING NGS VERSION 18.0. UNCOMMANDED SHIFT TO 4L
1. Parking Maneuvers/Gear Lever Transitions: Symptoms noted during a lower speed uncommanded shift to 4L event include:
After the initial event the vehicle may exhibit restricted vehicle top speed without 4X4 indicators illuminated but possible MIL (Malfunction Indicator Lamp) on.
a. Monitor contact plate A, B, C, D PIDS. With Mode Switch in;
If contact plate PIDS DO NOT correspond to the set 4WD position, check for continuity/shorts/moisture/corrosion in the vehicle side of the transfer case shift motor connector (toward the GEM). Visually inspect all terminals, pins, crimps, and connectors closely. Repair any wiring conditions in the contact plate circuits as necessary.
b. Road test at speeds above 16 km/h (10 mph) with Mode Switch in both A4WD and 4H to see if the condition returns. Clear DTCs from GEM (even if no DTCs exist) and cycle ignition. Clear DTCs from PCM if a P1270 (engine RPM or vehicle speed limiter reached) code was initially present.
UNCOMMANDED SHIFT TO 4H
1. Autolock Strategy (Expedition/Navigator Vehicles Only): Uncommanded 4H shifts with red 4X4 light on "solid" while driving in Automatic Mode may be a vehicle characteristic if driven off-road or under slippery conditions. The GEM Autolock strategy commands transfer case clutch to minimum duty cycle and engages 4H (with mode switch still set to A4WD) to prevent continuous cycling from prematurely wearing the clutch. This is design intent. Excessive tire circumference variations or axles with unmatched gear ratio's can result in unexpected Autolock function as well, for more detail check the speed sensor section below. Once the condition that caused Autolock strategy to activate is no longer present, the system stays in 4H until operator cycles the ignition key or sets the mode switch to 4H then back to A4WD.
b. If ratio's match and tire sizes are okay, check associated circuits and replace speed sensor(s) or repair wiring as necessary.
3. False Input to GEM A false mode switch input on circuits 682 and/or 780 (Dark Blue) could be interpreted by the GEM as operator requesting a 4H shift. Under this condition, the GEM will command shift motor movement to match the false switch input. The red 4X4 indicator will illuminate just as if operator moved the switch. If the condition became intermittent, the 4X4 range and also the indicators could switch back and forth between A4WD/2H and 4H.
a. Check the Mode Switch circuits between the GEM and dash for chafing, shorts, crimps, continuity. Repair wiring if faults found or if wiring okay, replace the Mode Switch itself.
STUCK IN 4L AT ALL TIMES
1. If the customer describes an uncommanded shift to 4L shortly after start-up and then unable to shift transfer case, check operation. Associated symptoms could include valid GEM DTCs being erased and false DTCs such as C1107 present. If stuck in 4X4, re-establishing normal shift motor operation is possible by putting mode switch in 4L with trans gear lever in neutral, service brake depressed, and vehicle stationary. To address: