When we partitioning a hard drive in Linux we set a System ID (for example 83) for a partition and when we want to mount it to a directory we use mkfs -t to format it and mount it to the directory. My question is why we don't format the partition when using fdisk command at first and we need to do it again?
fdisk only edits the partition table, not the file system. When you edit partitions, you are really editing the MBR (Master Boot Record) or GPT (GUID Partition Table) of the drive, and boot sectors of the partitions. The MBR/GPT contains the start and end sector boundaries for each partition (just the sector numbers where the partition starts and ends, not the actual partition space itself), and the type of partition it is (like type 83).
fdisk doesn't contain the necessary logic to create file systems.
When you run
mkfs, you are editing the file system within the start and end partition boundaries that were set by
fdisk in the MBR/GPT. For example, it creates block groups which hold things like the inode table to map file fragments on the partition to the file path.