This is a great question (honestly I think any questions about the former Soviet Central Asia are great questions)! Of course as one does I actually did a bit of reading recently on the subject that I would love to share.
There is a strong argument to be made that a Tajik national identity was not a salient feature of either what is now Tajikistan or Afghanistan before the 20th century. Those communities and individuals who would later identify as such were Sunni Muslims and Persian speakers (although often not monolingual ones), but also had identities more closely based on locality or economic profession than a national identity (tribal and clan identities were important for neighboring nomadic communities but that didn't really apply to the mostly agricultural and sedentary Persian speakers).
The Soviet Union pretty much is responsible for starting the nation-building project of a Tajik national identity in Tajikistan, under the idea of "nationalism in form, socialism in function", ie promoting national identities and corresponding institutions (a standardized national language, and local national elites) would develop these regions and bring them to a higher political and economic level able to participate in the rest of the union. Much of this was based on ideas of primordial, cohesive national identities, and this was promoted through national censuses and registration papers where citizens had to choose a single national identity (having multiple identities was apparently considered "transvestite", which is an interesting choice of words to say the least). The historiography of Tajiks reflected these assumptions (and the post-Soviet official historiography still does), by connecting modern day Tajiks as the descendants of the ancient pre-Islamic Sogdian people, who also traced their first statehood to the Muslim Samanid Empire of the 9th century. Tajiks were given an autonomous Soviet Socialist Republic within the Uzbek SSR that was eventually elevated to its own SSR status in 1929, although in a bitter irony two major cultural centers of the Persian Tajik tradition, Bukhara and Samarkand, were included in the Uzbek SSR (the Tajik SSR eventually got Khujand in the Fergana Valley as a sort-of compromise). It also confusingly included a number of Pamiri peoples who spoke East Iranian languages (closer to Pashtun and ironically Sogdian) rather than Persian.
Even the term "Tajik" came in for a reinvention. The etymology isn't totally clear, but it seems to be a work that essentially means "others" - as in, it refers to someone else, rather than being a name for self-identification. Official discourse said the name in fact came from the Persian word for "throne". The term was used by the Soviets to replace the older term "Sart", which referred to sedentary and urban inhabitants of the region who more often than not were multilingual and may or may not have had tribal affiliations: a distinctly un ethnic identity. Soviet authorities pushed that Persian speakers were Tajiks, and Turkic speakers were Uzbeks, and bilingual speakers literally had to pick one of those two national identities. I need to stress that these identities could get very fluid: Sadriddin Aini was an bilingual intellectual from Bukhara who in his youth identified with the Turkic-language jadid reform movement, and later in life became Tajikistan's national poet, which involved him having to leave Bukhara to move to the new Tajik SSR. Aini himself modeled his later "Tajik" writings on the "pure and simple" language of the new republic rather than the "bourgeois", refined Persian of Central Asian cities.
Despite the big grievances against Russia and Uzbekistan for the perceived "loss" of Samarkand and Bukhara, universal primary education and the national project proved to be pretty successful at building a Tajik national identity in the Tajik SSR. This held despite local power structures exploding into a full-scale civil war in 1992-1997, which was powered in part by resentment at the Soviet era republican elites who came from Khujand (which was solidly Tajik but also seen as a little too-Uzbekified). There were gripes in the Tajik SSR about fighting "brethren" in Afghanistan during the Soviet-Afghan War, but it is to the Tajik identity in Afghanistan that we will turn to next.