See the steps in the article If your iCloud Contacts, Calendars or Reminders aren't syncing - Apple Support
Set up and use iCloud Contacts - Apple Support
Take help of a web browser and enter in www.icloud.com with the apple id and password used in an iPhone , the contacts that were saved in will be visible select the desired contacts and click on gear icon in the left bottom corner and click on export as v card .
Open finder , click on downloads , right click on .vcf file and open with contacts .app( default ) .
The address book will get opened , a pop up window will appear stating are you sure you want to add number of cards , click on add .
Again open your address book the contacts that were in the iPhone will be transferred under the section “ All contacts “ in the Mac address book .
Sign out from www.icloud.com used with Apple ID and password for an iPhone .
Then enter in www.icloud.com with the apple id and password used in the Mac , click on gear icon and click on import as v card all the contacts will be transferred in the iCloud server , click on create group on the bottom right corner an untitled folder will be created in the side bar and the contacts from iPhone are transferred to the iCloud server account .
See some articles iCloud: Export a contact as a vCard from iCloud Contacts
iCloud: Import a contact into iCloud Contacts
iCloud: Create a group in iCloud Contacts
See this thread Get Contacts data back on Mac ? - Apple Community