The origins of this phrase appear to be from horse racing, where two or more horses that are evenly matched might run closely together towards the finish line, side by side. When this happens, the horses are said to be 'neck and neck.'

This saying goes back to at least the early to mid-19th century. At that time, it was a term commonly used in horse races. For example, the earliest I could find of this phrase in writing comes from the Tasmanian and Port Dalrymple Advertiser newspaper, printed in 1825, and it is indeed being used in connection with a horse race:

    "The owners rode their respective horses, and the race was said to be the finest ever witnessed on this turf, 
     both horses keeping neck and neck round the course."

​Today, this expression might be used in comparisons, or in reference to other types of competitions that are close, such as a basketball game.

N-Letter Sayings  |  Know Your Phrase, The Meanings and Origins For Phrases and Common Idioms
* Ericka made a fresh batch of oatmeal and chocolate chip cookies this morning, and they smell great. In terms of flavor, I'd say they are both neck and neck, but if I had to pick a winner, I'd go with the chocolate chip. - The Meanings and History For Popular Idioms and Phrases
Note: The origins for most common idioms cannot be said with a certainty. What's provided are theories that may be plausible to how a phrase originated, but not necessarily so. 

In addition, quotes that contain a phrase may be taken from old newspapers, poems, or books that were written centuries ago, but this by no means shows they originated from these. In all likelihood, if an expression is being used in a newspaper, it's probably already well known, and thus, from an older period of time.
N-Letter Sayings
Example Sentences:
Phrases and Idioms
1. A horse racing term that refers to two or more horses that are running side by side with each other.

2. Competitions or comparisons that are very even.

Example: If someone compared the gas mileage between two cars, and they both are about the same, then it might be said that these two cars are 'neck and neck' in terms of their gas mileage.
Two horses that are racing.