The history of football in Britain is useful here, as it was the first competitive team sport in the modern period for which there were standardized rules. Now, various ball games have been played all over the world since very ancient times. In Britain, medieval "football" was radically different from what we see today in the Football League. There were basically no rules, the teams were not fixed and could include hundred of people, even whole towns, and the "pitch" could be miles in extent, stretching through village into the surrounding countryside. One "goal" might be a church door a mile down the road, the other a particilar dock at the other end of town. The "game"--if we can even call it that--surged through the streets, houses, and fields, and could boil down to simply crowds of people pushing and battling to move a ball one way or the other, like a massive rugby scrum. You can see an example of a modern version of this here. Of course, this description is probably on the extreme end, and you there were probably much tamer versions of the game as well. The big violent episodes get all the attention though. In addition, this game is likely the ancestor of the various version of football we know today, Association Football (soccer), the different versions of rugby, even perhaps American, Australian, and Gaelic football.
James Walvin is a well-known social historian of sport in Britain and--not surprisingly--his interpretation of the tranformation of football turns on industrialization and class conflict. For him, the key is that the industrial bourgeoisie of the mid-19th century simply will not tolerate this kind of public disorder and DEFINITELY wouldn't allow it in the new industrial cities. As such, the game--and other early modern rural pursuits, including blood sports like bear-baiting--was supressed in the late 18th and early 19th centuries.
British public schools (for rich people, not "public" as in "provided by the state for all") in the 19th century picked up versions of the game though, often following ideas of "muscular Christianity" that became popular at the same time. These ideas held that the foundation for a truly moral existence were both physical and spiritual, and thus held that young men should engage in physical training (and competition, but more on that below). This resulted in many English and Scottish public school developing football programs, but of course at this point there were no standardized rules, so either each program would only play internally, or the rules would have to be negotiated for any match against another school. In 1848, a group of public schools met and agreed on the Cambridge rules, the first collective set of rules. At issue were the very fundamentals of the game: handling the ball, dribbling versus running with it, forward and backward passes, off-sides, and so on. Over the next few decades there would be a series of revisions and schisms that resulted in Rugby and, in 1863, the creation of the Football Association, and the modern game of football that we know.
Now, to your question about nationalism, I think it's necessary to recognize that these games were formalized at the same time that nationalism was becoming an increasingly important part of European political life. So, it impossible to disassociate them, and we should note that "international" matches didn't take long at all--The first international match was held in Glasgow between England and Scotland in 1872. Now, whether the rise of the popularity of these games is connected to "tribal" identities is much more difficult, not least because it's such a difficult term to define. The idea of it being about civic pride I find unconvincing; for me, it's really about the market and about nature.
I have to step away now; I've been writing this on my phone in the vet's office waiting room, and now I've got to bring the cat in to see the vet. I'll come back and finish this in a while. I have much more though, including my own hypothesis for the development of sport in the 19th century, which will be the subject of my next research project (once I finish bread).
EDIT: Unfortunately, life intervened all afternoon and through the evening, so I can't expand further at the moment. I also have a busy teaching week ahead, so my theories on the development of professional sports will have to wait for another day. I hope the answer so far is helpful.