The value of the "uma" and "um" here is in emphasizing the feminine and masculine nouns and retaining this in memory. Using the "a" before pencil here is optional but since it is in the Portuguese one might just humor the situation and use it in the English. While it is optional in English, as pointed out by Jayeless, it does distinguish between two separate meanings which the out of context sentence does not inform us otherwise.
Well, unless you're a linguistic prescriptivist, it kind of does...? In linguistics (at least in Australia, where I've studied it) the emphasis is always on what seems natural or unnatural to native speakers, not on artificial standards.
As for why you don't need the second "a" here - to me either sentence is grammatical, but mean different things. "Do you have a pen or a pencil?" is like, "I know you have one, now tell me which." Whereas "Do you have a pen or pencil?" means you don't know if they have either, but you'd like to borrow something to write with. The second sentence is more useful so that's why I put in that one. It's certainly wrong to say you NEED the second "a" there, because I'm sure I've heard this sentence (in English) dozens more times without it than with. (In fact, I'm not sure I've ever heard it with.) So, Duolingo should fix that :P