مَا ذَهَبَ إِلَّا زَيْدٌ
Only Zayd went. زَيْدٌ is the subject, so it is marfu'.
مَا رَأَيْتُ إِلَّا زَيْدًا
I saw only Zayd. زَيْدًا is the object, so it is mansub.
لَمْ أَذْهَبْ إِلَّا مَعَ زَيْدٍ
I went only with Zayd. زَيْدٍ is mudaf ilayh after مَعَ, so it is majrur.
The same word has three endings because its grammatical role changes.