Well, initially, Obi-Wan was covered by a hooded cloak, so R2-D2 wouldn’t recognize him (thus making it quite reasonable to be afraid).

We might assume that as soon as Obi-Wan’s face was visible, R2-D2 would have recognized him. Consider, though, that it has been 19 years since R2-D2 last saw Obi-Wan. It’s fairly plausible that he wouldn’t have recognized him immediately. Droids do have superior computing capabilities, but they aren’t better at everything than humans. It seems plausible that an astromech droid would not necessarily have flawless (or even very good) facial recognition routines. Perhaps it really did take him a few seconds to recognize Obi-Wan, given how much the latter had aged over the course of two decades.
Something similar might have explained Obi-Wan possibly not recognizing R2-D2 at first.
It’s also possible that we (and perhaps Obi-Wan) are misinterpreting R2-D2’s emotions, and what might seem to be a Binary squeak of fear might actually be an exclamation of disbelieving excitement ("I can’t believe it’s really you again!").
Regardless, they certainly did recognize each other eventually (despite Obi-Wan being cagey about it):
The R2 unit moved to Ben’s side as if it were his faithful pet—an
image that was further reinforced by the old man’s stroking a hand
over its domed head.
“It’s good to fly with you again, my old friend,” the old man said, so
softly Han wasn’t sure he had heard him right. But it wasn’t Han’s
business either way. He knelt down to pile his tools into their box
and went back to his work.
The Princess, the Scoundrel, and the Farm Boy