W.r.t. the US, visas were introduced by the Immigration Act of 1924, which declares that
§2. (a) A consular officer upon the application of any a co immigrant
(as defined in section 8) may (under the conditions hereinafter
prescribed and subject to the limitations prescribed in this Act or
regulations made thereunder as to the number of immigration visas
which may be issued by such officer) issue to such immigrant an
immigration visa which shall consist of one copy of the application
provided for in section 7, visaed by such consular officer. Such visa
shall specify (1) the nationality of the immigrant; (2) whether he is
a quota immigrant (as defined in section 5) or a non-quota immigrant
(as defined in section 4); (3) the date on which the validity of the
immigration visa shall expire; and (4) such additional information
necessary to the proper enforcement of the immigration laws and the
naturalization laws as may be by regulations prescribed.
In other words, it came about by act of Congress. The intent was to limit immigration into the US, and was one of many laws regulating immigration and naturalization passed since the founding of the US.