In John 1:6–8, John the Baptist was being questioned by the Jewish religious leaders about whether he literally was Elijah returned from heaven. In answer to that question, John said "no" because he was not literally Elijah.
In the second passage, Jesus is answering a question from the disciples about the fulfillment of prophecy. The disciples want to know why the scribes say that Elijah must return before the messianic age. Jesus answers that Elijah has returned, and the disciples understand him to be referring to John the Baptist. In this case, Jesus is speaking metaphorically. John the Baptist is not literally Elijah, but his heralding the arrival of the Messiah fulfills the messianic prophecy concerning Elijah.