The Northern Irishman was not at his fluent best at Emirates Golf Club, but showed all his battling qualities to eke out a round of 70 for an 11 under par aggregate total.
That was good enough for a one shot lead over Koepka, who produced a flawless seven under par display which moved the former Challenge Tour player up to second place on ten under.
McIlroy, who won his debut European Tour event at the 2009 Omega Dubai Desert Classic, opened with a bogey but rallied with three birdies to reach the turn in 33.
After a further birdie at the tenth, the World Number Seven looked like opening up a commanding advantage over the field, but dropped shots at the 13th and 16th holes have given hope to the chasing pack.
McIlroy said: "Even though I didn't play as well as I would've liked today, I'm still leading the golf tournament. I've been in this position before and I've gone on to win, so I know what it takes. I've led from the front quite well in tournaments that I have won, and I feel like I'm still playing well. So I'm expecting to out there and shoot two good scores over the weekend, and hopefully I can lift the trophy."
Koepka, who won three times on the Challenge Tour in his breakthrough season last year, has endured a slow start to the current campaign, but showed his class with seven birdies to set up the prospect of an intriguing battle with McIlroy over the weekend.
He said: "I've never played with Rory, so it's going to be fun. I know him a little bit from seeing him around, so hopefully I'll be able to play as well as I did today.
"I putted really well today. Yesterday I didn't putt very well, but today my putter came through for me. I've striking the ball really well, and I also chipped well today. So everything's going well, I can't complain."
Koepka's round of 65 was matched by England's Danny Willett, who carded eight birdies to climb into a share of third place on eight under par alongside Ireland's Damien McGrane and Frenchman Julien Quesne (both 70).
World Number One Tiger Woods posted a one over par 73 to fall back to a share of 44th spot on three under.
Brett Rumford leads the Australians in a share of 20th position although Scott Hend made a big move early on day two when he actually reached the lead before dropping five shots in his last five holes. He is in 44th position.
Leading Scores after round 2:
133 R McIlroy (Nir) 63 70;
134 B Koepka (USA) 69 65;
136 J Quesne (Fra) 66 70; D Willett (Eng) 71 65; D McGrane (Irl) 66 70;
137 T Jaidee (Tha) 68 69; S Gallacher (Sco) 66 71; J Donaldson (Wal) 69 68; J Walters (RSA) 69 68; H Stenson (Swe) 70 67; R Rock (Eng) 67 70; E Molinari (Ita) 65 72;
138 F Molinari (Ita) 69 69; R Kakko (Fin) 69 69; E Grillo (Arg) 71 67; S Dyson (Eng) 69 69; M Carlsson (Swe) 69 69; M Madsen (Den) 71 67; S Hansen (Den) 67 71;
139 G Bourdy (Fra) 71 68; R Sterne (RSA) 66 73; J Van Zyl (RSA) 71 68; T Olesen (Den) 71 68; J Luiten (Ned) 70 69; B Rumford (Aus) 69 70; C Doak (Sco) 71 68; S Kim (Kor) 70 69; S Kjeldsen (Den) 68 71; P Lawrie (Sco) 68 71; M Siem (Ger) 72 67;
140 M Baldwin (Eng) 66 74; R Karlsson (Swe) 73 67; T Lewis (Eng) 71 69; R Jacquelin (Fra) 69 71; P Waring (Eng) 70 70; B Wiesberger (Aut) 70 70; J Campillo (Esp) 68 72; K Broberg (Swe) 71 69; M Crespi (Ita) 69 71; E De La Riva (Esp) 70 70; A Wall (Eng) 74 66; R Cabrera-Bello (Esp) 71 69; C Montgomerie (Sco) 70 70;
141 D Fichardt (RSA) 69 72; A Quiros (Esp) 69 72; F Couples (USA) 70 71; M Ilonen (Fin) 69 72; L Slattery (Eng) 70 71; R Wattel (Fra) 68 73; A CaÃƒÂ±izares (Esp) 74 67; A Levy (Fra) 69 72; T Woods (USA) 68 73; M Kieffer (Ger) 71 70; S Webster (Eng) 71 70; S Hend (Aus) 69 72; H Otto (RSA) 68 73; S Noh (Kor) 69 72;
142 J Lima (Por) 71 71; C Del Moral (Esp) 70 72; S Benson (Eng) 72 70; S Kapur (Ind) 72 70; G Havret (Fra) 70 72; D Van Der Walt (RSA) 72 70; F Zanotti (Par) 72 70; P LarrazÃƒÂ¡bal (Esp) 74 68; S Jamieson (Sco) 73 69; G Stal (Fra) 74 68; T BjÃƒÂ¶rn (Den) 72 70; P Casey (Eng) 70 72; M Hoey (Nir) 70 72; C Wood (Eng) 73 69;