There are a couple of possible reasons for Ambrose Bierce to include the idea that Peyton Farquhar believes that he must have fallen asleep while walking. One relates to developing a sense of realism. This is significant because Bierce is exploring cognition and the perception of time in An Occurrence at Owl Creek. Another is foreshadowing for the ending. A surprise ending requires clues in the form of foreshadowing so the reader has no sense of being betrayed or "tricked" by the author. Another is that Bierce is, on one hand, describing a dream state and, on the other hand, a typical lapse in recall for events that become automatic, like struggling to walk, walk, walk to salvation.
Bierce writes Owl Creek in a straightforward, embellished style with a vocabulary that includes few adjectival and adverbial modifiers; he is not trying for a tone or a mood of romanticism. He is writing realistically. Saying that Peyton Farquhar thinks he must have fallen asleep while walking as an explanation for why he fails to remember a long part of his journey is part of Bierce's construction of realism both in terms of the natural world and in terms of cognitive experience and perception of time.
Falling asleep while walking employs foreshadowing in more than one regard. Sleep has a direct correlation to dreaming. Including this idea foreshadows the fact that Farquhar is, in his dream-like cognitive state and expanded sense of time, dreaming his escape. Sleep is also a symbol for both death and the dead. This symbolic association foreshadows the surprise ending, which is Farquhar's death. There is a correlated symbolism that also foreshadows his death. In the fifth sentence of the story, Bierce has Farquhar standing on railroad sleepers (railway support structures). Sleep symbolizes death and the dead; Farquhar stands on "death." This also foreshadows his ultimate end.
Finally, the inclusion of the idea of sleeping while walking supports the questions of cognition and experience of time that Bierce is exploring. If Farquhar's escape were true (it isn't...), then the introduction of this idea represents exploration of cognition in moments of extreme stress through which Bierce posits a description of a theory of cognition. In addition, Bierce uses this idea to posit a theory of the dream state relating to unconscious and conscious notice of detailed observance.