Morocco implicitly recognized the United States in 1777 by opening its ports to American ships.
Sultan Mohammed III issued a decree granting American ships protection and free access to Moroccan ports on June 23, 1777. This made Morocco the earliest country listed as recognizing the United States, even though the recognition was initially implicit rather than expressed through a formal treaty.
Morocco later formalized relations with the United States through the Treaty of Peace and Friendship, signed in 1786. The treaty remains an important early example of American diplomacy with a foreign power.
France is a common alternative answer because it formally recognized the United States and signed treaties of alliance and commerce in 1778. The Netherlands also gave an early foreign salute to the American flag in 1776, but that event is not the same as formal recognition by a government.