What you're describing that NNs do in the third paragraph is also done in linear regression, logistic regression, and many other supervised learning methods. (SVMs do it too, but in a more subtle way.) It may not be obvious in the case of linear regression because the value of the weights is given by an explicit formula (rather than an iterative procedure), but that formula represents the value of those weights that minimize the cost function between what you're calling $y$ and $\hat{y}.$ Neural Nets do the same thing, but because there isn't an explicit solution to the weights that minimize the cost function, it searches for that minimum through an iterative procedure.
Minimizing the cost function does not achieve the same thing as testing the accuracy of the model on a separate test set. It's true that, when you finish training a model on a training set, the model you now have is the one that will get the best prediction values on that same training set (given the assumptions of the model), where by "best", I mean the predictions that would give the lowest result of the cost function. However this says little about how well the model will generalize to other data outside your training set, which is ultimately your goal. It is possible that a very strong fit to the training set can be a result of overfitting, which means that the value of the cost function evaluated on a test set will be much higher than that on the training set. The test set is meant to check to see if you are overfitting. I argue that it is important for any supervised learning method, including neural nets.
Here's one analogy I like to use to illustrate the concept of overfitting. Imagine you're a professor who will be giving an exam tomorrow. You have two students, student $A$ and student $B.$ To student $A$ you give a practice exam with which to study. The practice exam has no overlapping problems with the actual exam, but tests an understanding of similar concepts. To student $B$ you give a copy of the actual exam for studying. After they both take the exam and you grade it, which of the two results is a better indicator of that student's grasp of the concepts, or their ability to take future exams in the subject?