As a new DM, this has been a learning process for me as well. Here is how I personally have improved (and am continuing to improve).
Spend longer before the game than you think is necessary. Approaching this like a writer has helped me significantly- make sure you write down the NPC's motivations, recent history, personality, and other details. Anything that may matter should be written down- do they have family in the area? How do they act around their friends? Is that different to how they act around strangers? These kinds of questions help you nail down their personality.
Almost all of this information is for you and you alone; your players will never see it directly.
Part of this process is envisioning what kinds of reactions players will have to an NPC. Consider what kinds of questions or actions they will take with the NPC around and write down how the NPC would react (or what they would say). Writing it down will help you remember later, and if it is a big enough problem you can keep these notes handy during the encounter (I sometimes do).
Make note cards. Particularly for recurring NPCs, make a note card of the most important facts about their personality. Pull the card out for interactions and refer to it if you're struggling. This really helps with brain fog, especially if your game sessions are long or you have any other reason to forget things (a cold, little sleep, etc).
Here is a sample card idea I did last night. It is two sided, one for "details" and one for "aw crap what was this guy doing again?" Details:
Piando: Fighter- Battle Master, dwarf. Kicked out of his clan due to family feud. Stripped of his surname. Works to restore honor to himself and be welcomed back. Stereotypical dwarf. Prefers ale. Usually friendly, open, honest, and lively. He has a very serious side he rarely shows. Will not tell just anyone about his past.
Wears intricately carved plate armor and wields a +2 Maul (named Brownie, which is an inside joke with his fellows) that criticals on 17-20. He has a brown beard he keeps trimmed because he almost died in battle when his opponent grabbed his beard. He regularly shaves the top of his head.
"Aw crap" side:
Piando. Experienced Dwarf fighter. Lawful good. AC 20, +2 Maul (Brownie). Honorable, no accent, friendly.
Hires players to destroy powerful undead and restore his personal honor and hopefully be accepted back into his clan.
{List of powers/abilities Piando can do in combat and PHB page numbers for them}
Know the character. Once all that is done, test yourself. Set a random alarm on your phone with a single hint. "Piando," "Dwarf," "Bald bearded guy" are possible examples. From a simple hint you should be able to recall the character and all important details (basic physical description, any accent or mannerisms, basic personality traits). If you can't remember these when the alarm goes off, you need to review your notes.
My experience has been that once you do this, you'll remember the characters and how they act. For me, this makes surprise questions much easier, as I understand the character enough to just roll with it.