Does a creature's ability trigger while it's in the hand or graveyard?
No.

Unless specifically stated otherwise in the ability text, or they could only function from another zone (such as in Flashback), abilities of creatures, lands, artifacts, etc., only work while they are permanents, i.e. when they are on the battlefield.

Also note that Ostracize does not even target creatures, it only targets opponent players, so it could never trigger the Agent.